enthused_confused Posted March 6, 2018 Share Posted March 6, 2018 I have a html select input element for the states of the United States. When the user clicks the triangular icon of the select element I would like the drop-down to display all of the state options in alphabetical order if the form hasn't been submitted. After submission I would like the select element to display a $_SESSION['var'] . The $_SESSION ['var'] would be the state that was selected.. Say for instance the user leaves the page or the user is redirected to page with another form then the forms are pre-populated with the user's previous inputs. I have been able to accomplish on all my other input elements, but not the SELECT element I have tried the following code for the select options: <label for="req-state"><span><i class="glyphicon glyphicon-home"></i></span> State *</label> <select id="req-state" name="req-state" class="form-control" placeholder="please select your state" > <option>Please select your state</option> <option value="Alabama"<?php if($_SESSION && $_SESSION['State'] == "Alabama") echo "SELECTED"; else echo " ";?>>Alabama</option> <option value="Alaska" <?php if($_SESSION && $_SESSION['State'] == "Alaska") echo "SELECTED";?>>Alaska</option> Screen shot of html select input element This is how I have parsed the form input: $stateList = array("Alabama", "Alaska", "Arkansas", "American Samoa", "Arizona", "California", "Colorado", "Connecticut", "District of Columbia", "Delaware", "Florida", "Georgia", "Guam", "Hawaii", "Idaho", "Illinois", "Indiana","Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland","Massachusetts", "Michigan", "Minnesota", "Missouri", "Mississippi", "Montana", "Nebraska", "Nevada","New Hampshire", "New Jersey", "New Mexico", "New York","North Carolina","North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Puerto Rico", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Virgin Islands", "Washington", "West Virginia","Wisconsin", "Wyoming"); if($_POST['req-state']){ $selectedState = $_POST['req-state']; $selectedState = trim(stripslashes($selectedState)); } if(strlen($selectedState) === 0){ //Blank string, add error to $errors array. $errors[] = "Please select a state."; } if (strlen($selectedState) < 4 || strlen($selectedState) >20) { $errors[] ='Invalid selection'; } $filteredState = filter_var($selectedState, FILTER_SANITIZE_STRING,FILTER_FLAG_NO_ENCODE_QUOTES ); if(!in_array($filteredState,$stateList)){ $errors[] = "Please select a state"; } else{ //echo '<span style="color:green;">Success. You have selected</span>'.' '.'<b>'.$filteredState.'</b>'.'</br>'; } $State = $filteredState; $_SESSION['State'] = $State; Note: If user selects one of the options and leaves the page upon return the select input shows the proper state selection. Same is true if user goes to page with different form that has $_SESSION['State'] variable as value. Link to comment Share on other sites More sharing options...
Barand Posted March 6, 2018 Share Posted March 6, 2018 <option value="Alabama"<?php if($_SESSION && $_SESSION['State'] == "Alabama") echo "SELECTED"; else echo " ";?>>Alabama</option> <option value="Alaska" <?php if($_SESSION && $_SESSION['State'] == "Alaska") echo "SELECTED";?>>Alaska</option> Seriously? You have an array of states, so why not loop through that to build the options and set the selected flag $opts = "<option value=''>Please select a state</option>" foreach ($stateList as $state) { $sel = $state==$_SESSION['state'] ? 'selected' : ''; $opts .= "<option $sel>$state</option> } Link to comment Share on other sites More sharing options...
enthused_confused Posted March 6, 2018 Author Share Posted March 6, 2018 @Barand Thanks for taking time to look at this. Where would I insert the suggested code? Link to comment Share on other sites More sharing options...
enthused_confused Posted March 7, 2018 Author Share Posted March 7, 2018 I have tried like <select><?php......?></select> Link to comment Share on other sites More sharing options...
enthused_confused Posted March 7, 2018 Author Share Posted March 7, 2018 Can anybody help me out here? I am not having any success trying to use the code suggested Barand. I am not sure how insert in my current code. Link to comment Share on other sites More sharing options...
requinix Posted March 7, 2018 Share Posted March 7, 2018 Post what you tried and explain what makes it look like it's not working. Link to comment Share on other sites More sharing options...
enthused_confused Posted March 7, 2018 Author Share Posted March 7, 2018 After some tweaking I was able to get the code working. Had to add some quotation marks and semicolons. Had to add some echo as well. Had to include $statelist array. working code looks like this: <select id="req-state" name="req-state" class="form-control" placeholder="please select your state"> <?php $stateList = array("Alabama", "Alaska", "Arkansas", "Arizona", "California", "Colorado", "Connecticut", "District of Columbia", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana","Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland","Massachusetts", "Michigan", "Minnesota", "Missouri", "Mississippi", "Montana", "Nebraska", "Nevada","New Hampshire", "New Jersey", "New Mexico", "New York","North Carolina","North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Virgin Islands", "Washington", "West Virginia","Wisconsin", "Wyoming"); $opts = "<option value=''>Please select a state</option>"; echo $opts; foreach ($stateList as $state) { $sel = $state==$_SESSION['state'] ? 'selected' : ''; $opts .= "<option $sel>$state</option>"; } echo $opts; ?> </select> Thanks Barand. Thanks Requinix for having a look as well. Link to comment Share on other sites More sharing options...
cyberRobot Posted March 7, 2018 Share Posted March 7, 2018 @enthused_confused - One minor note. The <select> tag doesn't have a "placeholder" attribute. This <select id="req-state" name="req-state" class="form-control" placeholder="please select your state"> Should be changed to this <select id="req-state" name="req-state" class="form-control"> A list of supported attributes can be found here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select Also note that you can validate your HTML code using the following tool: https://validator.w3.org/ Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.