petenaylor Posted November 14, 2010 Share Posted November 14, 2010 Hi all I am trying to get my drop down menu to use a session variable as the selected menu option. Here's my code: echo "<option value=\"\">Delivery Area</option>"; $selected = ""; if(isset($_SESSION['postal_area']) && $_SESSION['postal_area'] == "England") { $selected = "selected"; } elseif(isset($_SESSION['postal_area']) && $_SESSION['postal_area'] == "Wales") { $selected = "selected"; } elseif(isset($_SESSION['postal_area']) && $_SESSION['postal_area'] == "Scotland") { $selected = "selected"; } else { $selected = ""; } echo " <option value=\"England\" selected=\"".$selected."\">England</option>"; echo "<option value=\"Scotland\" selected=\"".$selected."\">Scotland</option>"; echo "<option value=\"Wales\" selected=\"".$selected."\">Wales</option>"; echo "</select>"; However, this shows the 'Wales' one all the? Please help! Many thanks Pete Quote Link to comment Share on other sites More sharing options...
marcus Posted November 14, 2010 Share Posted November 14, 2010 <?php $areas = array('England','Scotland','Wales'); echo '<select name="postal">'; echo '<option>Delivery Area</option>'; foreach($areas AS $area){ $selected = (isset($_SESSION['postal_code']) && $area == $_SESSION['postal_code']) ? ' SELECTED' : ''; echo '<option value="'.$area.'"'.$selected.'>'.$area.'</option>'; } echo '</select>'; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.