Solved

Spry Validation Select Place in values from mySQL database

Posted on 2008-06-16
9
406 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now