Jump to content

How to get html select element to show original value then a $_SESSION[' '] variable value.


Recommended Posts

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 elementpost-205086-0-68311900-1520374273_thumb.png

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

 

<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

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

@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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.