Solved

Spry Validation Select Place in values from mySQL database

Posted on 2008-06-16
9
404 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

762 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

16 Experts available now in Live!

Get 1:1 Help Now