-1

i have a simple drop down menu which is senting data to a database..

this is the code example (not real values, just for everyone to get my problem)

<div class="card-body">
  <form name="Choice" method="get">
  <div class="Choice">
    <label for="inputStatus">What color are your eyes</label>
    <select id="inputStatus" name="EyeColor" class="form-control">
    <option value="Brown">My eyes are brown</option>
    <option value="Green">My eyes are green</option>
    <option value="Blue">My eyes are blue</option>
    </select>
  </div>
  </br>
  <div class="row">
    <div class="col-12">
      <input type="submit" value="<?=$lang['Submit']?>" class="btn btn-info float-right">
      </button>
    </div>
  </div>
  </form>
</div>

Data are sent normally to the Database , but after i press submit i want the value of the dropdown menu to stay(show) in the menu that i just chose.

DFd
  • 97
  • 8
  • 2
    Assuming the options are looped for output, then compare the value of the retrieved `EyeColor`. If the value matches, set a `selected` attribute for that option. – Paul T. Jan 30 '22 at 22:44
  • Does this answer your question? [Keep values selected after form submission](https://stackoverflow.com/questions/2246227/keep-values-selected-after-form-submission) – CBroe Jan 31 '22 at 08:16

1 Answers1

0
<?php 
$lastValue="Brown";//mysqli->query 
?>
<div class="card-body">
  <form name="Choice" method="get">
  <div class="Choice">
    <label for="inputStatus">What color are your eyes</label>
    <select id="inputStatus" name="EyeColor" class="form-control">
    <option <?=$lastValue=="Brown":"selected"?> value="Brown">My eyes are brown</option>
    <option <?=$lastValue=="Green":"selected"?> value="Green">My eyes are green</option>
    <option <?=$lastValue=="Blue":"selected"?> value="Blue">My eyes are blue</option>
    </select>
  </div>
  </br>
  <div class="row">
    <div class="col-12">
      <input type="submit" value="<?=$lang['Submit']?>" class="btn btn-info float-right">
      </button>
    </div>
  </div>
  </form>
</div>