Remembering input from dynamic drop down lists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stylus
Forum Newbie
Posts: 17
Joined: Fri Dec 16, 2005 9:22 am

Remembering input from dynamic drop down lists

Post by stylus »

Hello,

I am trying to figure out how to make my form remember what was selected from a drop box when a user submits it but has to go back and correct their inputs.

Here is the code that I am using that needs to be remebered.

Code: Select all

<select name=location_name OnChange=document.workorderform.submit()><option> </option>";
while($row = mssql_fetch_array($results)) {
echo '<option value="'.$row[CustomerID].'">'.$row[CustomerName].'</option>'; 
}
echo "</select>";

Here is some code for some "static" drop boxs I am using, it works good because the optional values are always the same. but in the dynamic list it is choosing any number of options from a list of over 2200 customer names.

Code: Select all

<?php echo '<select name="performed_by">'; 
echo "<option value='Service'"; if($performed_by == 'Service') echo " selected"; echo ">Service</option>";
echo "<option value='Operations'"; if($performed_by == 'Operations') echo " selected"; echo ">Operations</option>";
echo "<option value='Copier Administative'"; if($performed_by == 'Copier Administrative') echo " selected"; echo ">Copier Administrative</option>";
echo "<option value='Furniture Administrative'"; if($performed_by == 'Furniture Administrative') echo " selected"; echo ">Funiture Administative</option>";  
echo "</select>"; 
?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

how are your users "going back" to the form page? With the "back" button I presume...if so, there's not much you can do if their browsers dont' remember what the selected option was.

if you're posting back to the page, you can do this:

Code: Select all

echo "<select name=\"location_name\" OnChange=\"document.workorderform.submit()\"><option> </option>"; 
while($row = mssql_fetch_array($results)) { 
echo "<option value=\"".$row[CustomerID]."\" ".(isset($_POST['location_name']) && $_POST['location_name'] == $row['CustomerName'] ? "selected=\"selected\"" : "").">".$row['CustomerName']."</option>"; 
} 
echo "</select>";
stylus
Forum Newbie
Posts: 17
Joined: Fri Dec 16, 2005 9:22 am

Post by stylus »

Thanks for the help, it works like you said. My form post back to itself for all the error checking, then once the user confirms everything the data is posted to an action page.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

in that case, what I posted should work for you...
Post Reply