Hi folks,
I'm building a form over several pages. The basic structure of it is that each page submits to itself via POST, checks for errors in the form field, then if none are found saves the POST variables to the session, and then forwards to the next page.
The trouble is that the variables from the first page are lost. There seems to be two sessions - one on the first page, and one on all the others, which is why I use the
session_id() or session_start()
code at the beginning of each script, but that still seems to result in two different sessions being created.
Here's the code (minus a lot of content, but with enough to get the idea), any help would be much appreciated:
<?php
ini_set("session.gc_maxliftime","5400"); //it's a long form
session_id() or session_start();
$submitted = false;
$errors = false;
$surnameError = false;
$firstnameError = false;
if(isset($_POST['_submit_check']))
{
$submitted = true;
if($_POST['surname'] == "")
{
$surnameError = true;
}
if($_POST['firstname'] == "")
{
$firstnameError = true;
}
if($surnameError || $firstnameError )
{
$errors = true;
}
else
{
foreach($_POST as $k => $v)
{
$_SESSION[$k] = $v;
}
session_write_close();
/*foreach($_SESSION as $k => $v)
{
echo "<p>".$k." = ".$v."</p>";
}
*/
$url = "http://www.exampleurl.com/phptest/form_2.php?SID=".session_id()."&";
header("Location: ".$url);
}
}
?>
<head>
<!-- html head stuff -->
</head>
<body>
<?
if($errors)
echo"<div class=\"centercontent\">
<fieldset class=\"errorfield\">
<p class=\"label\">There were some errors on this page. Please review the fields <span class=\"errorlabel\">specified in red.</span></p>
</fieldset>
</div>";
?>
<div id="content">
<form name="mainform" id="mainform" action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="_submit_check" value="1" />
<!-- PERSONAL DATA SECTION -->
<div class="sectionheader">
Personal Data
</div>
<!-- SURNAME -->
<fieldset>
<div class="leftcontent">
<p class="<? echo ($surnameError ? "errorlabel" : "label"); ?>">Surname:</p>
</div>
<div class="rightcontent">
<input type="text" name="surname" <? if($submitted) echo "value=\"".$_POST['surname']."\""; else echo "value=\"fname\""; ?> />
</div>
<!-- FIRST NAME -->
<div class="leftcontent">
<p class="<? echo ($firstnameError ? "errorlabel" : "label"); ?>">First / Middle Names:</p>
</div>
<div class="rightcontent">
<input type="text" name="firstname" <? if($submitted) echo "value=\"".$_POST['firstname']."\""; else echo "value=\"fname\"";?> />
</div>
</fieldset>
</form>
</div>
</body>
</html>