Echo 'selected' in html5 dropdown form using PHP

1.8k views Asked by At

I have a form that fetches user info into the $row variable so forms are populated if the row exists if the database, but then saves any new input through the $_POST global variable, in case the user makes a mistake elsewhere on the form. It all works great.

    <label for="qualification2">Grade:</label>
        <input type="text" name="qualification2" id="grade2" class="form-control" 
        value="<?php if(isset($_POST['qualification2'])) { 
        echo $_POST['qualification2']; 
        } else { echo ($row ['qualification2']); } ?>">

I would like to do similar using a selection dropdown box but the code doesn't seem to work the same with dropdown boxes and I wonder what I'm doing wrong or if this is even possible.

I've also tried echo from the $row variable but that doesn't work either

    <?php if($row['qualMonth2'] == 'February') { echo ' selected'; } ?>

This is my code:

    <select input type="text" name="qualMonth2" id="qualMonth2" value="Select Month">
         <option value="January" <?php if(isset($_POST['qualMonth2']) == 'January') { echo ' selected'; } ?>>January</option>
          <option value="February" <?php if(isset($_POST['qualMonth2']) == 'February') { echo ' selected'; } ?>>February</option>
          <option value="March" <?php if(isset($_POST['qualMonth2']) == 'March') { echo ' selected'; } ?>>March</option>
          <option value="April" <?php if(isset($_POST['qualMonth2']) == 'April') { echo ' selected'; } ?>>April</option>
          <option value="May" <?php if(isset($_POST['qualMonth2']) == 'May') { echo ' selected'; } ?>>May</option>
          <option value="June" <?php if(isset($_POST['qualMonth2']) == 'June') { echo ' selected'; } ?>>June</option>
          <option value="July" <?php if(isset($_POST['qualMonth2']) == 'July') { echo ' selected'; } ?>>July</option>
          <option value="August" <?php if(isset($_POST['qualMonth2']) == 'August') { echo ' selected'; } ?>>August</option>
          <option value="September" <?php if(isset($_POST['qualMonth2']) == 'September') { echo ' selected'; } ?>>September</option>
          <option value="October" <?php if(isset($_POST['qualMonth2']) == 'October') { echo ' selected'; } ?>>October</option>
          <option value="November" <?php if(isset($_POST['qualMonth2']) == 'November') { echo ' selected'; } ?>>November</option>
          <option value="December" <?php if(isset($_POST['qualMonth2']) == 'December') { echo ' selected'; } ?>>December</option>
      </select>
3

There are 3 answers

0
Devon Bessemer On

I prefer to perform these checks in Javascript (I prefer JQuery), I think it cleans up the code, but you could also simplify this greatly by using a loop with generated months.

Loop through 1-12 for output

<select input type="text" name="qualMonth2" id="qualMonth2" value="Select Month">
<?php
for($i=1; $i<=12; $i++) {
    $dateObj   = DateTime::createFromFormat('!m', $i);
    $monthName = $dateObj->format('F');
    echo "<value>" . $monthName . "</option>";
}
?>
</select>

This approach takes a DRY (Do not repeat) approach. If you need to modify something, you only need to modify 1 line of code versus 12.

Jquery for choosing value (defaults to January if $_POST['qualMonth2'] is not set):

<script>
     $('#qualMonth2').val("<?php echo (isset($_POST['qualMonth2'])) ? $_POST['qualMonth2'] : 'January' ?>");
</script>

Take what you will from this answer, I just showed you two possibilities, you can certainly avoid Javascript by adding a check in the loop if you want.

1
Pogrindis On

Lets look at what you wrote..

<option value="February" <?php if(isset($_POST['qualMonth2']) == 'February') { echo ' selected'; } ?>>February</option>

What you're saying is.. If there is a value set for qualMonth2 (true / false), compare that against a string..

So you're comparing true || false against a string.

isset($_POST['qualMonth2']) returns true // false.

So none will ever be selected.

Refactor it..

<?php 
 $month = "";

 if(isset($_POST['qualification2'])){
   $month = $_POST['qualification2'];
 }
?>

Then use $month as the comparative value.

<option value="July" <?php if($month == 'July') { echo ' selected'; } ?>>July</option>
0
Thamilhan On

isset validates to true or false based on if variable is set or not. You can't check the value just with isset.

Use a small utility function to check condition:

function checkSelected($value) {
    if(isset($_POST['qualMonth2']) && $_POST['qualMonth2'] == $value) {
        return true;
    } else {
        return false;
    }
}

Your select element should be then:

<select input type="text" name="qualMonth2" id="qualMonth2" value="Select Month">
    <option value="January" <?php echo checkSelected('January') ? 'selected':'' ?>>January</option>
    <option value="February" <?php echo checkSelected('February') ? 'selected':'' ?>>February</option>
    <option value="March" <?php echo checkSelected('March') ? 'selected':'' ?>>March</option>
    <option value="April" <?php echo checkSelected('April') ? 'selected':'' ?>>April</option>
    <option value="May" <?php echo checkSelected('May') ? 'selected':'' ?>>May</option>
    <option value="June" <?php echo checkSelected('June') ? 'selected':'' ?>>June</option>
    <option value="July" <?php echo checkSelected('July') ? 'selected':'' ?>>July</option>
    <option value="August" <?php echo checkSelected('August') ? 'selected':'' ?>>August</option>
    <option value="September" <?php echo checkSelected('September') ? 'selected':'' ?>>September</option>
    <option value="October" <?php echo checkSelected('October') ? 'selected':'' ?>>October</option>
    <option value="November" <?php echo checkSelected('November') ? 'selected':'' ?>>November</option>
    <option value="December" <?php echo checkSelected('December') ? 'selected':'' ?>>December</option>
</select>