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 Link to comment https://forums.phpfreaks.com/topic/218660-drop-down-menu-selected-using-session-variable/ 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>'; ?> Link to comment https://forums.phpfreaks.com/topic/218660-drop-down-menu-selected-using-session-variable/#findComment-1134155 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.