This question already has an answer here:
This is my php code:
<select name="month" id="select1">
<option value="">Select Month</option>
<?php
foreach ($monthArray as $month) {
$monthPadding = str_pad($month, 2, "0", STR_PAD_LEFT);
$fdate = date("M", strtotime("2016-$monthPadding-01"));
echo '<option name="month" value="'.$monthPadding.'">'.$fdate.'</option>';
}
?>
</select>
I want to keep the selected month in dropdown list even after submiting form. Now dropdown list shows months from april to november (apr,may,jun,jul,aug,sep,oct,nov).
</div>
Maybe this helps. It's basically the same. You send data with GET and "get" them back with js.
Remove your below code..
echo '<option name="month" value="'.$monthPadding.'">'.$fdate.'</option>';
And Replace it with the one below...
echo '<option name="month" value="'.$monthPadding.'"';
if($_POST['month']==$monthPadding){
echo ' selected';
}
echo '>'.$fdate.'</option>';
Use the code below before your dropdown...
$month = '';
if(isset($_POST['month'])){
$month = $_POST['month'];
}
And use $month
when comparing