dimanche 5 septembre 2021

Foreach loop undefined index

I am writing a code where I've made HTML checkboxes and then use php script (checkform.php) to produce a foreach loop listing what items were selected. I can't get the loop to work no matter what I do, and I'm not sure what I'm missing here. I've tried pre-defining the array or creating an empty one for the values to go into which also does not work. I Need help please!

<form action="checkform.php" method="post">
          <p><fieldset><legend>Register Your Interests</legend>
<!-- checkbox input to select options -->
            <label><input type="checkbox" name="interests[]" id="Gauge" value="Gauge"> Gauge </label>
            <br><label><input type="checkbox" name="interests[]" id="thermometer" value="thermometer"> Thermometer </label>
            <br><label><input type="checkbox" name="interests[]" id="wind" value="wind"> Wind</label>
            <br><label><input type="checkbox" name="interests[]" id="lightning" value="lightning">Lightning</label>
            <br><label><input type="checkbox" name="interests[]" id="hygro" value="hygro"> Hygro </label><br>
if(!empty($_POST["interests"]))
  {
    echo "You have chosen the following workshops:";
    foreach($_POST["interests"] as $checked)
    {
      echo "<p>" . $checked ."</p>"
    }
  }
  else
  {
    echo"You have not chosen a workshop, but we add new workshops all the time. We'll keep you updated by e-mail.";
  }



Aucun commentaire:

Enregistrer un commentaire