0

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>' ;
?>
16
  • 1
    Use isset() with a conditional, and make sure session_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. Commented Nov 22, 2014 at 17:32
  • 1
    Post your entire code, this means your HTML form. Again, guesswork. Commented Nov 22, 2014 at 17:47
  • 1
    I don't see form tags nor a named submit button. Please show us your actual working code. This btw, if ( !empty($_POST['submit'])) you should use isset(). You also could use bracing {...} around your conditionals. Commented Nov 22, 2014 at 18:02
  • 1
    Besides a missing </form> tag, I am unable to reproduce the warnings, even after adding the missing tag in my test file. Commented Nov 22, 2014 at 18:18
  • 1
    You do have session_start(); in the next page, right? You're also outputting before header in your second page. Commented Nov 22, 2014 at 18:22

3 Answers 3

5

You're outputting before header in your second page, which would have thrown something similar to:

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /path/to/file.php:4) in /path/to/file.php on line 9

Nothing should be above <?php - no HTML, spaces, etc.

You need session_start(); in there also.

session_start(); is required to be inside all files using sessions.

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();

    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>' ;
?>

<b>Dietary Requirements: </b>
<br><br>
Sign up to request clarification or add additional context in comments.

Comments

2

You have two ways to choose :

1- make sure that user fills all the input you need , unless he/she cannot submit the form or he submits but you can prevent the submiting by javascript and make sure all fields are filled out

You can achieve this by adding required attribute to your inputs , though this will not work in some browsers (maybe IE )

Or you can check all of them using Javascript .

2- you can change you code like this :

       if ( isset($_POST['gluten'])) 
        $_SESSION['gluten'] = $_POST['gluten'];

NOTE :

when you use empty , PHP assumes that there is a for example $_POST['gluten'] , and then checks if its value is empty or not

But if that variable hasn't been set , it will throw errors , so you must first check if is set (isset).

in the response of your Edit :

assume you want to show a checkBox that is either checked or not , yes ?

for example :

   if(myCondition){

     $checkeck = "checked='checked'";

   }else{
     $checked = "";

   }

then you can use this variable like this :

   <input type="checkbox"  <?php echo $checked?> />

1 Comment

Thanks for your reply, this removes the errors however on the thank you page, the checked boxes are all echoed, even if the user makes 1 selection. I have updated my question with the code present on the thank you page which is meant to display the dietary checkboxes
0

You can also give the inputs in html an required attribute:

<input type="..." required />

And a style Tip:

if ( isset($_POST['submit'])) { 
    $_SESSION['vege'] = $_POST['vege'];
    $_SESSION['vegan'] = $_POST['vegan'];
    $_SESSION['peanut'] = $_POST['peanut'];    
    $_SESSION['gluten'] = $_POST['gluten'];
} else {
    ...
}

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.