0

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>
7
  • is $row populated ? Care to share it ? Also please dear god refactor this. Commented Dec 14, 2016 at 18:11
  • 1
    Do you know what does isset() return? Commented Dec 14, 2016 at 18:11
  • Are you trying to use PHP client side? By the time the page has been sent to the client, the PHP code doesnt exist. Commented Dec 14, 2016 at 18:11
  • @Carcigenicate what ??? Commented Dec 14, 2016 at 18:12
  • @Pogrindis Nvm, it looked like he was trying to run PHP code client-side. Commented Dec 14, 2016 at 18:13

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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>

1 Comment

This makes the most sense to me, it's not actually working still but I now feel like it's something to do with my code, perhaps elsewhere. I will keep trying, thank you for your help
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.