Using Session Tracking for Multipage Form in PHP

1.9k Views Asked by At

I'm trying to display a multiple page form that preserves submitted data from one page to the next using session tracking. $_POST['stage'] determines which form should be displayed. Each form has a hidden input type with a value set to increment the $stage variable by 1 but when I submit the data from the first form, the value of $stage seems to remain the same as I don't see the next form. Sessions is enabled in php.ini.

Here's my example:

<?php
session_start();

//Determine which integer to assign to the stage
if (($_SERVER['REQUEST_METHOD'] == 'GET') || (!isset($_POST['stage']))) {
    $stage = 1;
} else {
    $stage = (int) $_POST['stage'];
}

//Save any submitted data
if ($stage > 1) {
    foreach ($_POST as $key => $value) {
    $_SESSION[$key] = $value;
    }
}
?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>My Form Example</title>
    </head>
    <body>
    <?php if ($stage == 1) { ?>
        <form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>
            <label for='firstField'>First field:</label>
            <input type='text' name='first_field /><br />
            <input type='hidden' name='stage' value='<?php echo $stage + 1 ?>' />
            <input type='submit' value='Next' />
        </form>
    <?php } else if ($stage == 2) { ?>
        <form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>
            <label for='secondField'>Second field:</label>
            <input type='text' name='second_field /<br />
            <input type='hidden' name='stage' value='<?php echo $stage + 1 ?>' />
            <input type='hidden' value='Done' />
        </form>
    <?php } ?>
    </body>
</html>
3

There are 3 best solutions below

1
vee On

Try adding session_start() at the top of your page. That's the first thing I've noticed.

0
Gimme5 On

I discovered a typo on my working script. Hindsight, I should have copied and pasted my entire script. Sorry. This script works fine (with session_start() at the beginning which still doesn't appear after I post the question).

0
Stefano Mtangoo On

Since you didn't mention what a problem is, which made me to sift and find exactly, I will put more precise answer. The problem is no ending single quote in the lines

 <input type='text' name='first_field /><br />

and

<input type='text' name='second_field /<br />

and so my final working script is

<?php
session_start();
 $stage = 0;
//Determine which integer to assign to the stage
if (!isset($_POST['stage'])) {
    $stage = 1;
} else {
    $stage = (int) $_POST['stage'];
}

//Save any submitted data
if ($stage > 1) {
    foreach ($_POST as $key => $value) {
        $_SESSION[$key] = $value;
    }
}
?> 

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>My Form Example</title>
    </head>
    <body>
    <?php if ($stage == 1) { ?>
        <form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>
            <label for='firstField'>First field:</label>
            <input type='text' name='first_field' /><br />
            <input type='hidden' name='stage' value='<?php echo $stage + 1 ?>' />
            <input type='submit' value='Next' />
        </form>
    <?php } else if ($stage == 2) { ?>
        <form action='<?php echo $_SERVER['SCRIPT_NAME'] ?>' method='post'>
            <label for='secondField'>Second field:</label>
            <input type='text' name='second_field' /<br />
            <input type='hidden' name='stage' value='<?php echo $stage + 1 ?>' />
            <input type='hidden' value='Done' />
        </form>
    <?php } ?>
    </body>
</html>