Welcome to WebmasterWorld Guest from 23.20.137.66

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Form Question

posting selection from db results in echoing ID instead of desired val

     
2:26 am on Jul 2, 2009 (gmt 0)

5+ Year Member



In the following form, I call on the db to populate a dropdown box in a form. The form goes to confirmation.php where I want to echo the information back. When I try this, it only echos the ID of the table, which is not the value I would like to see (i need to see the patient name that was selected from the dropdown.
Here's the code on the form page:

$query = mysql_query("SELECT `ID`,`FIRST_NAME`,`LAST_NAME`FROM `patients`ORDER BY `LAST_NAME` ASC")
or die (mysql_error());
echo "<select name='name'>\n";
echo "<option value='' selected></option>\n";
while ($data = mysql_fetch_array($query, MYSQL_ASSOC))
{
echo " <option value='{$data['ID']}'>{$data['LAST_NAME']},
{$data['FIRST_NAME']}</option>\n";
}
echo "</select>\n";

And here is the code on the confirmation:


$name = $_POST['name'];
echo $name;
8:00 am on Jul 2, 2009 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Unfortunately that is the way it kinda works. The value of the options VALUE attribute is returned as the value of the $_POST['name'] variable.

To get the LAST_NAME you need to look the ID up again in your DB, or perhaps save these results in a temporary file if it was a very big query?

OR you could incorporate the LAST_NAME as part of the VALUE attribute? ie:

<option value="ID-LASTNAME">LASTNAME</option>

When the form gets returned you can explode() [uk2.php.net] the value into its two parts:

$patient = explode('-',$_POST['name']); 
$id = $patient[0];
$lastname = $patient[1];
6:38 pm on Jul 2, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Not for sure if I understand you but is it something like this?
$query = mysql_query("SELECT `ID`,`FIRST_NAME`,`LAST_NAME`FROM `patients`ORDER BY `LAST_NAME` ASC")
or die (mysql_error());
echo "<select name='name'>\n";
echo "<option value='' selected></option>\n";
while ($data = mysql_fetch_array($query, MYSQL_ASSOC))
{
echo " <option value=\"{$data['FIRST_NAME']} $data['LAST_NAME']}\">{$data['LAST_NAME']},
{$data['FIRST_NAME']}</option>\n";
}
echo "</select>\n";

if the user selected Smith, Bob that would return Bob Smith for the POST value

5:02 am on Jul 3, 2009 (gmt 0)

5+ Year Member



that worked penders thanks
 

Featured Threads

Hot Threads This Week

Hot Threads This Month