Forum Moderators: coopster

Message Too Old, No Replies

Form Help: Remember Value Of Select Statement

Simple way without a database

         

stidj

9:46 pm on Apr 23, 2004 (gmt 0)

10+ Year Member



I have a select menu in a form....I would like to have the previously selected option value pre-selected on a submit on the form....

I don't know any other way to do this other than to have an individual variable like this:

<option name=name value=value $onevariableforeachoption>

where I would have a lot of if statements to see if the value is "name" and if it is then $onevariableforeachoption will be = to "selected"....

there has got to be a better way someone please let me know :)

thanks so much again everyone

Netizen

10:41 pm on Apr 23, 2004 (gmt 0)

10+ Year Member



I would set up an array for the possible values and loop over that to produce the select menu

$values=array('cat','dog','horse');

foreach ($values as $value) {
printf('<option name="name" value="%s" %s>',
$value,$value==$_POST['name']?'selected':'');
}

Untested code but it should work.

A less obscure way is:

foreach ($values as $value) {
if ($value==$_POST['name']) {
$selected='selected';
} else {
$selected='';
}
printf('<option name="name" value="%s" %s>',
$value,$selected);
}