PHP multi page form with sessions

I am unable to find a basic example of how to do the following, and would appreciate some help.

I have a long form (572 fields) that I want to break up into about 10 pages, using PHP sessions to carry data from page to page as the user fills out the form.

I would rather not have to do anything that involves hidden form fields or assigning all 572 fields to the session manually (this thing is huge). So, I was hoping I could to do it like so using an array:

<?php session_start();

foreach($_POST as $k=>$v) {
$_SESSION['post'][$k]=$v;
}

 ?>

My problem is this: How do I keep adding values from page to page to the session? I can get a session to carry values from page A to page B, but when I fill out the form on page B and go to page C, the values from page A are lost.

I need to know how to append values from forms fields on each page to the session, so that at the end of the form, I have all 572 form fields stored in the session and can do with them what I wish.

Any advice is greatly appreciated.

If you are defining unique form field names and calling session_start(); before any output, than everything should work fine.

That’s what I was thinking! Here’s what I have as a simple test:

Page 1

<?php session_start();

foreach($_POST as $k=>$v) {
$_SESSION['post'][$k]=$v;
}

 ?>

<form action="2.php" method="post" enctype="multipart/form-data">
<fieldset>
<label for="first">First Name</label>
<input name="first" id="first" type="text" size="25" />
<br />
<label for="last">Last Name</label>
<input name="last" id="last" type="text" size="25" />
<br />
<input name="Go" type="submit" value="Go" />
</fieldset>
</form>

Page 2

<?php session_start();

foreach($_POST as $k=>$v) {
$_SESSION['post'][$k]=$v;
}
print_r($_POST);
 ?>
<form action="3.php" method="post" enctype="multipart/form-data">
<fieldset>
<label for="color">Color</label>
<input name="color" id="color" type="text" size="25" />
<br />
<input name="Go" type="submit" value="Go" />
</fieldset>
</form>

Page 3

<?php session_start();
foreach($_POST as $k=>$v) {
$_SESSION['post'][$k]=$v;
}
print_r($_POST);
 ?>
<body>
<p>done</p>
</body>


Results of print_r($_POST); from each page:

Page 2: Array ( [first] => john [last] => doe [Go] => Go )

Page 3: Array ( [color] => Red [Go] => Go )

Somehow I am losing Page 1’s values when I submit page 2.

Well, yes, because all you’re looking at there is the contents of the POST array - try checking the session instead:

print_r($_SESSION);

:wink:

Woohoo! Thanks for confirming what I knew all along: I’m an idiot!