0

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>
brasofilo
  • 25,496
  • 15
  • 91
  • 179
Gimme5
  • 1
  • 2

3 Answers3

1

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

vee
  • 38,255
  • 7
  • 74
  • 78
  • I have that written in my coded example but it's not showing up when I post the question for some reason. It's the first line of code after my backtick. – Gimme5 Jul 13 '13 at 20:21
0

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).

Gimme5
  • 1
  • 2
0

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>
Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93