Page 1 of 1

Reselect Correct <Select> Option

Posted: Thu Oct 07, 2010 11:21 am
by marcdd2
I have a drop down menu that displays the desired radius in miles for the user’s search. The form posts the data and then reloads the page. I want the drop down menu to continue showing the last distance that they selected. Currently I have the top option as “within” and the value as previously selected, but I would like it to just select the right spot of the drop down. Anyone know how to do this?

Code: Select all

<select name="radius" id="radius">
<OPTION VALUE="<?php echo $radius; ?>">within</Option>
<OPTION VALUE="1">1 Miles</Option>
<OPTION VALUE="2">5 Miles</Option>
<OPTION VALUE="3">10 Miles</Option>
<OPTION VALUE="4">25 Miles</Option>
<OPTION VALUE="5">50 Miles</Option>
</select>

Re: Reselect Correct <Select> Option

Posted: Thu Oct 07, 2010 11:52 am
by twinedev
You need to check what they submitted against the value you are setting for each option:

Code: Select all

Within <select name="radius" id="radius">
<?php 
    $arySet = array(1=>1,2=>5,3=>10,4=>25,5=>50);
    foreach ($arySet as $key=>$val) {
        echo '<option value="',$key,'" ';
        if ($radius==$key) { echo 'selected="selected" ';
        echo '>',$val,' Mile',(($val>1)?'s':''),'</option>',"\n";
    }
?>
</select>
(Also, a personal preference of mine, it has code to add make Mile for 1 Mile, and Miles for all the rest)

-Greg