Keep values selected after form submission

Viewed 168261

Consider:

<form method="get" action="">
   <select name="name">
      <option value="a">a</option>
      <option value="b">b</option>
   </select>
   <select name="location">
      <option value="x">x</option>
      <option value="y">y</option>
   </select>
   <input type="submit" value="Submit" class="submit" />
</form>

On submitting the form, how do I make sure that the selected values remain selected in the dropdowns? This form is inside WordPress (PHP).

12 Answers

To avoid many if-else structures, let JavaScript do the trick automatically:

<select name="name" id="name">
   <option value="a">a</option>
   <option value="b">b</option>
</select>

<script type="text/javascript">
  document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>

<select name="location" id="location">
  <option value="x">x</option>
  <option value="y">y</option>
</select>

<script type="text/javascript">
  document.getElementById('location').value = "<?php echo $_GET['location'];?>";
</script>
<select name="name">
   <option <?php if ($_GET['name'] == 'a') { ?>selected="true" <?php }; ?>value="a">a</option>
   <option <?php if ($_GET['name'] == 'b') { ?>selected="true" <?php }; ?>value="b">b</option>
</select>

I don't work in WordPress much, but for forms outside of WordPress, this works well.

PHP

location = "";  // Declare variable

if($_POST) {
    if(!$_POST["location"]) {
        $error .= "Location is required.<br />"; // If not selected, add string to error message
     }else{
        $location = $_POST["location"];          // If selected, assign to variable
     }

HTML

<select name="location">
    <option value="0">Choose...</option>
    <option value="1" <?php if (isset($location) && $location == "1") echo "selected" ?>>location 1</option>
    <option value="2" <?php if (isset($location) && $location == "2") echo "selected" ?>>location 2</option>
</select>

Just change this line:

 <input type="submit" value="Submit" class="submit" />

with this line:

 <input type="submit" value="Submit/Reload" class="submit" onclick="history.go(-1);">
<select name="name">
    <?php $options = ["a", "b", "c", "d", "e"];
        foreach($options as $o){                            
            if($o == $_GET['name']) $attr="selected='true'";
            else $attr = "";
            echo "<option value='{$o}' {$attr}>{$o}</option>";
        } 
    ?>
</select>

We can create an array of option before hand and only use simple if/else to check if selected or not. If you want different values for data and different value for displaying option you can use associative arrays too.

<select name="name">
    <?php $options = ["a"=>"Option A", "b"=>"Option B", "c"=>"Option C", "d"=>"Option D", "e"=>"Option E"];
        foreach($options as $index=>$o){                            
            if($index == $_GET['name']) $attr="selected='true'";
            else $attr = "";
            echo "<option value='{$index}' {$attr}>{$o}</option>";
        } 
    ?>
</select>
Related