homepage Welcome to WebmasterWorld Guest from 67.202.56.112
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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




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

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;

 

penders




msg:3944620
 8:00 am on Jul 2, 2009 (gmt 0)

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];

andrewsmd




msg:3945071
 6:38 pm on Jul 2, 2009 (gmt 0)

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

reffik024




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

that worked penders thanks

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved