I am designing a form for a restaurant website which allows users to book a dinner reservation, each field is validated (apart from the checkboxes) and if the user has entered the correct information in all fields they are sent to a thank you page which displays their reservation details.
There is an issue within my code to do with the checkboxes present on my site, if the user hits submit without filling in the required fields, the following errors get displayed at the top of the screen:
"Notice: Undefined index: vege in /home/users/2014/xxx/public_html/xxx/index.php on line 17
Notice: Undefined index: vegan in /home/users/2014/xxx/public_html/xxx/index.php on line 20
Notice: Undefined index: peanut in /home/users/2014/xxx/public_html/xxx/index.php on line 23
Notice: Undefined index: gluten in /home/users/2014/xxx/public_html/xxx/index.php on line 26"
Here is the code for my reservations page:
<?php
session_start();
if ( isset($_POST['vege']))
$_SESSION['vege'] = $_POST['vege'];
if ( isset($_POST['vegan']))
$_SESSION['vegan'] = $_POST['vegan'];
if ( isset($_POST['peanut']))
$_SESSION['peanut'] = $_POST['peanut'];
if ( isset($_POST['gluten']))
$_SESSION['gluten'] = $_POST['gluten'];
?>
...
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
...
<strong>Dietary Requirements:</strong>
<br><br>
Vegetarian <input type="checkbox" name="vege" value="Vegetarian" <?php if(isset($_POST['vege'])) echo "checked='checked'"; ?>>
<br><br>
Vegan <input type="checkbox" name="vegan" value="Vegan" <?php if(isset($_POST['vegan'])) echo "checked='checked'"; ?>>
<br><br>
Peanut Allergy <input type="checkbox" name="peanut" value="Peanut Allergy" <?php if(isset($_POST['peanut'])) echo "checked='checked'"; ?>>
<br><br>
Gluten Allergy <input type="checkbox" name="gluten" value="Gluten Allergy" <?php if(isset($_POST['gluten'])) echo "checked='checked'"; ?>>
<br><br><br>
<input type="submit" id="submit" name="submit" value="Submit">
Even though these errors are shown, the code performs the duty it should. Does anyone know how I can fix my code to prevent the error messages from showing? If I need to show more code for clarity I can update my question for you. Thank you!
EDIT
The errors are now removed however the checkboxes arent being echoed properly on the thank you page, as no matter what the user selects, all of the dietary requirements are echoed :( Is there any way I can change my code below so that only the checked checkboxes are echoed?
<b>Dietary Requirements: </b>
<br><br>
<?php
if(isset($_SESSION['vege']))
echo $_SESSION['vege'] . '<br>' ;
if(isset($_SESSION['vegan']))
echo $_SESSION['vegan'] . '<br>' ;
if(isset($_SESSION['peanut']))
echo $_SESSION['peanut'] . '<br>' ;
if(isset($_SESSION['gluten']))
echo $_SESSION['gluten'] . '<br>' ;
?>
isset()with a conditional, and make suresession_start();is loading and that your form elements are indeed named along with a POST method. Showing full code would take the guesswork out of things.if ( !empty($_POST['submit']))you should useisset(). You also could use bracing{...}around your conditionals.</form>tag, I am unable to reproduce the warnings, even after adding the missing tag in my test file.session_start();in the next page, right? You're also outputting before header in your second page.