I am trying to make a form using PHP that after pressing the submit button, displays the data a person has entered but I’m getting an error that states Undefined array Key “ticket4” in the display page due to Drodown menu and the 4th option of the checkbox not getting displayed on the display page.
Code for dropdown menu:
<p>Please select the number of tickets :</p> <select name "dropdown"> <option value="Select" >Select</option> <option value="ticket1" > 1 </option> <option value="ticket2" > 2 </option> <option value="ticket3" > 3 </option> <option value="ticket4" > 4 </option> </select><br><br>
Code for dropdown menu on display page:
The number of tickets that you have selected are : <?php if (isset($_POST["ticket1"])) { echo $_POST["ticket1"]; } else if (isset($_POST["ticket2"])) { echo $_POST["ticket2"]; } else if (isset($_POST["ticket3"])) { echo $_POST["ticket3"]; } else{ echo $_POST["ticket4"]; } ?><br>
Code for checkbox menu:
<p>Which movies would you like to watch :</p> <input type="checkbox" id="movie1" name="movie1" value="Stowaway"> <label for="movie1"> Stowaway </label><br> <input type="checkbox" id="movie2" name="movie2" value="Tenet"> <label for="movie2"> Tenet </label><br> <input type="checkbox" id="movie3" name="movie3" value="Home Invasion"> <label for="movie3"> Home Invasion </label><br> <input type="checkbox" id="movie4" name="movie4" value="Rush"> <label for="movie4"> Rush </label><br><br>
Code for checkbox menu on display page:
The movies which you would like to watch are : <?php if (isset($_POST["movie1"])) { echo $_POST["movie1"]; } else if (isset($_POST["movie2"])) { echo $_POST["movie2"]; } else if (isset($_POST["movie3"])) { echo $_POST["movie3"]; } else { echo $_POST["movie4"]; } ?><br>
Advertisement
Answer
You need the name
attribute in your select
element like so:
<select name="example">
On the back end you can use this (in this case “example”) as a reference:
$_POST['example']
.
the value is determined by the options value. For more information of the select element and how the name
attribute is used, see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select