Solved

Spry Validation Select Place in values from mySQL database

Posted on 2008-06-16
9
415 Views
Last Modified: 2008-06-16
Hi,

I've got a mySQL database and am accessinm through php.  I have a page which has Spry validation select objects on and i am wanting to populate these with values from the database.  I can access the database through php etc. although am unsure on how to pass these strings (as they happen to be) back into html and place them as values for a object.

Thanks
James
0
Comment
Question by:James_h1023
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 125 total points
ID: 21793713
Hi,

Selecting and outputting the options inside the spry select would be something like:

<span id="spryselect1">
<select name="select1" id="select1">
<?php
$query = 'SELECT field1 as value, field2 as text FROM mytable';
$res = mysql_query($query);
while($row = mysql_fetch_assoc($res)) {
  echo '<option value="'.htmlspecialchars($row['value']).'">'.htmlspecialchars($row['text']).'</option>';
}
?>
</select>
<!--Add an error message-->
<span class="selectRequiredMsg">Please select an item.</span>
</span>

Hope this helps,

-r-
0
 
LVL 4

Author Comment

by:James_h1023
ID: 21793723
Brilliant I shall give this a go :)
0
 
LVL 4

Author Comment

by:James_h1023
ID: 21793979
I have the attached although the object does appear there are no items in it.
I placed in echo '<option value="test">test</option>'; to see whether this would add, and even this doesn't add!
<span id="spryselect1">
<select name="select1" id="select1">
<?php>
// connect to database
$link = mysql_connect('localhost', 'XXX_', 'XXX');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
 
// select database
$db_selected = mysql_select_db('XXX', $link);
if (!$db_selected) {
die ('Can\'t use islebridgeclub_ : ' . mysql_error());
}	
			
$query = 'SELECT first name as value, first name as text FROM members';
$res = mysql_query($query);
while($row = mysql_fetch_assoc($res)) {
echo '<option value="'.htmlspecialchars($row['value']).'">'.htmlspecialchars($row['text']).'</option>';
echo '<option value="test">test</option>';
}
?>
</select>
<!--Add an error message-->
<span class="selectRequiredMsg">Please select a name.</span>
</span>

Open in new window

0
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
LVL 49

Expert Comment

by:Roonaan
ID: 21794018
Can you try it using:
$query = 'SELECT `first name` as value, `first name` as text FROM members';
$res = mysql_query($query) or print('<option>'.mysql_error().'</option>');
0
 
LVL 4

Author Comment

by:James_h1023
ID: 21794085
Excellent that works, must have been the 'first name' instead of first name!
How about going around the other way, so when they are clicked I can use the text of the item the user has selected and use this in php to place into a table?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21794110
When the form is posted you would could investigate the value of $_POST['select1'];

if(isset($_POST['select1'])) {
  $selectedMember = $_POST['select1'];
}

This requires you to have <form method="POST"

Sidenote: In my latest code, remove the or print('<option>'.mysql_error().'</option>') part, as I added it only for debugging purposes and should not be used on production.
0
 
LVL 4

Author Comment

by:James_h1023
ID: 21794172
Ah ok.
Don't quite understand the code there.  What is the if statement checking?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21794223
the if statement is checking if there actually exists a key 'select1' inside the $_POST array. The $_POST array holds all fields/values from your form post. If you try to access the $_POST['select1'] when it is not available, you will get a warning.
0
 
LVL 4

Author Comment

by:James_h1023
ID: 21794252
Thanks very much.
James
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question