Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Value="" variable in SELECT list

Status
Not open for further replies.

salewit

Programmer
Oct 31, 2002
58
US
I have a drop box in a PHP form script that has error checking in it. The user fills out the form, and if there is an error in the form, the user fixes the error, the script is re-run, but I can't seem to "hold" the value of the state drop-down. It defaults back to "Select One". How do I hold the last entered state dropdown selection?

Thanks,
Sam


State
<SELECT NAME="state" value="<?php echo $state; ?>">
<option value="">Select One</option>
<option value="AL">Alabama(AL)</option>
<option value="AK">Alaska(AK)</option>
<option value="AZ">Arizona(AZ)</option>
.... etc, etc


 
<select> items do not work the way you think they do.
The HTML has to have the word selected inside the option tag that is active.
You need to implement something like this:
Code:
$myStates = array('AL'=>'Alabama','AK'=>'Alaska','AZ'=>"Arizona");
print '<SELECT NAME="state">';
print '<option value="">Select One</option>';
# looping the array and building a dynamic dropdown
foreach($myStates AS $value=>$label){
   print '<option value="'.$value.'"';
   # check if it is the selected one
   if ($_POST['state'] == $value) print " selected";
   print ">$label</option>";
}
print "</select>";
Builg the items includes checking against the posted value and printing the keyword into the option tag.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top