Solved

Spry Validation Select Place in values from mySQL database

Posted on 2008-06-16
9
407 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

803 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