Reselect Correct <Select> Option

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
marcdd2
Forum Newbie
Posts: 4
Joined: Tue Apr 06, 2010 7:58 pm

Reselect Correct <Select> Option

Post 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>
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Reselect Correct <Select> Option

Post 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
Post Reply