'My Option Value Keeps Resetting After Submit
Hey everyone i have this option menu where you can choose between Day 1, 2 and 3 and once the submit button is clicked it triggers a controller which saves the data to phpmyadmin. The data is being saved okay. But the option menu keeps resetting to day 1 no matter what option is clicked.
<div class="form-group row">
<label class="col-md-4 col-form-label text-md-right" for="choose-day">Choose a Day:</label>
<div class="col-md-6">
<select class="form-control" name="days" id="days-option">
<option class="form-control" value="1">Day 1</option>
<option class="form-control" value="2">Day 2</option>
<option class="form-control" value="3">Day 3</option>
</select>
</div>
Solution 1:[1]
Because the form is posted you need to store the value and then add the selected statement to the option that has been chosen. ??
Note: your php post script should be running before the if($whatDay == 1) selected else the $whatDay doesn't have a value to use and it doesn't add selected to anything.
<?php
if (isset($_POST['post_Btn'])) {
$whatDay = $_POST['days'];
}
?>
<div class="form-group row">
<label class="col-md-4 col-form-label text-md-right" for="choose-day">Choose a Day:</label>
<div class="col-md-6">
<select class="form-control" name="days" id="days-option">
<option class="form-control" value="1" <?php if($whatDay == 1) { echo 'selected';} ?> >Day 1</option>
<option class="form-control" value="2" <?php if($whatDay == 2) { echo 'selected';} ?> >Day 2</option>
<option class="form-control" value="3" <?php if($whatDay == 3) { echo 'selected';} ?> >Day 3</option>
</select>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
