-3

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).

Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114

2 Answers2

2

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

0

Maybe this helps. It's basically the same. You send data with GET and "get" them back with js.

Keep values selected after form submission

Community
  • 1
  • 1
Tux
  • 39
  • 1
  • 11