Forum Moderators: coopster
My problem is with the select box. I've got a standard <select> field for the user to select their state. If the form contains errors, I'd like the state they selected to stay selected when the form reloads. But, the only way I've found to do this is very messy.
Here's the code I'm using, with the form pared down to only the <select> box and using only five states. With the whole list of states, the script seems to run slowly and it's just plain messy to deal with. Anyway, here it is:
<?php
// The following variables will later
// be used to add selected="selected"
// to the chosen state. For now, assign
// no value so there aren't any undefined
// variable errors.
$al = '';
$ak = '';
$az = '';
$ar = '';
$ca = '';
// If the form HAS been submitted,
// the $selected_state variable will
// take on the value of whatever choice
// the user made from the select box.
if (!empty($_POST))
{
$selected_state = "{$_POST['state']}";
}
// If the form HAS NOT been submitted,
// the $selected_state variable will
// add nothing.
else
{
$selected_state = '';
}
// Checking to see what choice was made
// from the select box, so the selected="selected"
// can be added to the chosen option
if ("$selected_state" == 'AL') {$al = " selected=\"selected\"";}
elseif ("$selected_state" == 'AK') {$ak = " selected=\"selected\"";}
elseif ("$selected_state" == 'AZ') {$az = " selected=\"selected\"";}
elseif ("$selected_state" == 'AR') {$ar = " selected=\"selected\"";}
elseif ("$selected_state" == 'CA') {$ca = " selected=\"selected\"";}
else { /* Do nothing */ }
// Here's the select box itself. Notice
// the variables at the end of each <option>
// opening tag. That will add selected="selected"
// to whatever state the user chose, and will
// add nothing to the other <option>'s.
$states_list = "
<select name=\"state\">
<option value=\"AL\"$al>Alabama</option>
<option value=\"AK\"$ak>Alaska</option>
<option value=\"AZ\"$az>Arizona</option>
<option value=\"AR\"$ar>Arkansas</option>
<option value=\"CA\"$ca>California</option>
</select>
";
// Finally, the form submits to itself.
echo '<form action="/this-script.php" method="post">';
echo "$states_list";
echo '<input type="submit" name="submit" value="Submit">';
echo '</form>';
?>
I've tried to explain what's going on in comments above, but briefly, the logic is to check which state has been selected, then add
to whichever state the user chose by means of the $al (or $ak, $az, etc.) variable in the <option> tag. selected="selected"
Using the whole list of states really slows things down due to all the variables and checking them against the submitted value. I'm sure there must be an easier way to do this, maybe with an array? Unfortunately I'm just not "with it" enough with PHP to get any kind of new thoughts on this. Any and all advice will be appreciated.
Thanks,
Matthew
$states = array('AL'=>'Alabama','AK'=>'Alaska', ...) and so on.
Then, to print out your select statement:
<select name="state">
<?php
foreach($states as $id=>$name){
if($selected_state == $id){
$sel = 'selected="selected"';
}
else{
$sel = '';
}
echo "<option $sel value=\"$id\">$name</option>";
}?>
</select>
I believe this will fit what you are trying to do.
-sned