Solved

Spry Validation Select Place in values from mySQL database

Posted on 2008-06-16
9
413 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Testrail - Active Directory integration. 4 34
Securing source code... theoretical question 10 45
How can I send attachment with mail 5 20
Format Date 7 23
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
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…

734 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