Question

Apologies if this is a dumb question, but I'm learning PHP right now and I'm having an issue regarding doing an isset check of a $_POST value that may or may not exist depending on whether the user has submitted a form or not.

If I do the following outside the context of a foreach loop, I get an undefined index message(when the form has not yet been submitted), as I would expect:

if ($_POST['dorm'] == "somethingrandom"){ ... }

As I understand it, I get this error because that $_POST value doesn't exist and I need to run it through an isset() or !empty() check instead of just assuming it exists. But if I have the exact same code inside a foreach loop, I do not get the error:

<form action="test.php" method="post">
<select name="dorm">
<?php foreach ($DORMS as $dorm){
    if ($_POST['dorm'] == $dorm){
        echo "<option selected value=\"$dorm\">$dorm</option>";
    } else {
        echo "<option value=\"$dorm\">$dorm</option>";
    }
}
?> // end php tag
</select>
</form>

Again, the above code creates no errors, even though I'm not doing any isset() or !empty() checks. I'm just wondering if this is the expected behavior, and if so, why?

Any clarification would be greatly appreciated! Thank you.

Was it helpful?

Solution

No notice or error inside foreach is expected when $DORMS is empty. Code inside foreach does not get executed when $DORMS is empty. When you have some value in $DORMS you can see that notice using chrome dev tools or firebug.

enter image description here

EDIT

Notice has nothing to do with foreach loop. You do not see the notice because it is generated inside a <select> element in html. Browser is not showing any normal text that is inside <select>. If your foreach loop was inside a <p> html element you could see that message as the browser would output that.

<p>
<?php foreach ($DORMS as $dorm){
    if ($_POST['dorm'] == $dorm){
        echo "<option selected value=\"$dorm\">$dorm</option>";
    } else {
        echo "<option value=\"$dorm\">$dorm</option>";
    }
}
?>
</p>

OTHER TIPS

you can check if the form is submitted by POST method in two ways

if($_POST){
   ...
}

or

if(isset($_POST['your_variable'])){
   ...
}

This may be happening because it is not getting into the loop

Try this

  <?php
    if (isset($_POST['dorm'])) 
  {
    $DORMS = $rows; //array from database
    foreach($DORMS  as $dorm){
         if($row['dorm'] == $_POST['dorm']){
              $isSelected = ' selected="selected"'; // if the option submited in form is as same as this row we add the selected tag
         } else {
              $isSelected = ''; // else we remove any tag
         }
         echo "<option value='".$row['id']."'".$isSelected.">".$row['name']."</option>";
       }
  }
    ?>

The only reason for this would be because the array $DORMS is empty. In that case, the foreach loop will never execute the body, so it will never try to access the nonexisting $_POST['dorm'].

What you should do is set another variable to the selected dorm, and protect that with an isset check:

$dorm_input = isset($_POST['dorm']) ? $_POST['dorm] : null;

Then in your loop check if ($dorm == $dorm_input)

As other says empty $DORMS may be the reason for not getting notice. For check on your code you can do something like .

if (is_array($DORMS) && sizeof($DORMS) > 0) {
    foreach ($DORMS as $dorm) {
        if (isset($_POST['dorm']) && $_POST['dorm'] == $dorm){
            echo "<option selected value=\"$dorm\">$dorm</option>";
        } else {
            echo "<option value=\"$dorm\">$dorm</option>";
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top