Jump to content

how to retain drop-down menu selected state when the page reposts.


louis_coetzee

Recommended Posts

It's common courtesy to ensure that a form with validation problems reposts with the fields still filled in, so the user doesn't have to start over, by echoing the variables in the Value attribute of the text fields, but I also want to have a drop-down menu to be sticky and retain its selected state when the page reposts.

It's basically a static dropdown on a registration page, it then posts, validates, if errors are detected, i need the person to fix the problems, but the drop-down boxes lose their values. I need to retain this.

$options = array ('One', 'Two', 'Three', 'Four');

echo "<select name=\"number\">\n";
foreach ($options as $optValue)
{
    $selected = ($optValue == $_POST['number']) ? ' selected="selected"' : '';
    echo "<option value=\"{$optValue}\"{$selected}>{$optValue}</option>\n";

}
echo "</select>\n";

 

EDIT: Corrected a couple errors

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.