Trying to get MCQ with checkboxes in SESSION

I’m trying to write the code for an online form:

On page 1 there are just some demographic questions. It’s on that first page I start a session. When they submit their answers they get redirected to page 2 where I place some statements on which they can select their level of agreement.

  • In case they want to go back their answers on the previous page are remembered thanks to the session.
  • Ultimatly their answers will be transferred to a database (haven’t written that yet).

Problem:

  1. I tried to multiply the amount of questions to 3 questions on page 2 but when I go to page 3 and return to page 2 the answers aren’t remembered?

  2. If I only check some of the questions (not all of them) I get ‘Notice: Undefined index:’ with the line the unanswered question is on. Can this be solved somehow?

This is the code from page 2:

<form action="page3.php" method="post">
<?php
    $options = array(
        'Good' => 'Good',
        'Neutral' => 'Neutral',
        'Bad' => 'Bad',
);
 
checkbox( 'Question_1', 'Question_1', 'How good is your health?', $options );
checkbox( 'Question_2', 'Question_2', 'How good is your math?', $options );
checkbox( 'Question_3', 'Question_3', 'How good is your knowledge of astrofysics?', $options );
 
?>
 
<?php submit('Go To Step 3 &raquo;'); ?>
</form>

This is the code from page 3 (where the info from page 2 is stored in the SESSION variables):

<?php
include_once('header.php');
 
// Store data from page 1 in session
if ( ! empty( $_POST ) ) {
  $_SESSION['Question_1'] = $_POST['Question_1'];
  $_SESSION['Question_2'] = $_POST['Question_2'];
  $_SESSION['Question_3'] = $_POST['Question_3'];
}
 
?>

And this is the code of the used function (from functions.php):

function checkbox( $name, $id, $label, $options = array() ) {?>
  <div class="form-group">
    <p><?php echo $label; ?></p>
    <?php foreach ( $options as $value => $title ) : ?>
      <label class="checkbox-inline" for="<?php echo $id; ?>">
        <input type="checkbox" required name="<?php echo $name; ?>[]" value="<?php echo $value; ?>" <?php isset($_SESSION['Question_1'],$_SESSION['Question_2'],$_SESSION['Question_3']) ? checked="checked":; ?>>
        <span class="checkbox-title"><?php echo $title; ?></span>
      </label>
    <?php endforeach; ?>
  </div>

What am I doing wrong?

Problem 1)
Handle each page as an separated form. Remove the action attribute so that your form starts with

<form method="post">

Now if the user hits the submit button the same php file will be called again but now in the POST request mode instead of the GET request mode. This is your one time change to validate and save your data to a place where you can store it for a longer period, e.g. the session OR the database.

if($_SERVER['REQUEST_METHOD'] == 'POST') {
     // if valdation is successfull
     // store it all into the session
}

If you want to store it into the session there are two important things to remember:

  1. You must start the session on each page with the session_start() function.
  2. You must use a unique array key for each value all over your entire website. This means that if you write $_SESSION[‘Question_1’] on page a and later op page b then page b will overwrite the value of page a!

While you are debugging your forms and/or sessions it can be helpfull to show the entire content of $_POST or $_SESSION. Which can be easily done with a print_r:

session_start();
echo '<h1>SESSION:</h1>';
echo '<pre>' . print_r($_SESSION, true) . '</pre>';
echo '<h1>POST:</h1>';
echo '<pre>' . print_r($_POST, true) . '</pre>';

Problem 2)
This error is literally telling you that you are trying to access an array key which does not exist.
with $_GET, $_POST and $_SESSION arrays you will NEVER know if they have a element under some kind of key or not. For example if the form is not submitted there will be no elements in the $_POST array. If a user starts after one hour on page 2 and the session is already expired then there wont be any form data available anymore from page 1. etc etc.
That’s why in many cases you should check if a certain element exists in the array. One example:

if(isset($_SESSION['Question_1']) {
    echo 'Question_1 exists!';
} else {
    echo 'Question_1 does not exist.';
}

Return to page 2 how? If you are using the back button you break things, you need to reload that page to get values from the server, otherwise you just see the page that the browser has in memory.

Sponsor our Newsletter | Privacy Policy | Terms of Service