Hi,
I recently adopted the foreach statement in a multi-form I am using as advised by one of the members here.
However, it does not appear to work - my form is 6 pages (1 file) which takes all fields from each page and carries them over using hidden fields (supposed to) and then dumps to db.
However, the dump to db is only dumping the last form fields page (page 5) - it is ignoring all the others (as if their values have been left blank)
This is how my script currently works (cut-down version):
echo "<form name='form1' action='form.php' method='get'>";
if (!isset($pageno))
{
$pageno = "1";
echo "Name:<input type='text' name='name'>";
echo "E-mail:<input type='text' name='email'>";
echo "<input type='submit' name='cmdGo' value='Next >>'>";
echo "<input type='hidden' name='pageno' value='$pageno'>";
echo "</form>";
}
else if ($pageno == "1")
{
$pageno = "2";
echo "<form name='form2' action='form.php' method='post'>";
echo "address:<input type='text' name='address'>";
echo "<input type='submit' name='cmdGo' value='Next >>'>";
echo "<input type='hidden' name='pageno' value='$pageno'>";
foreach($_POST as $key => $value) {
echo "<input type=\"hidden\" name=\"" . $key . "\" value\"" . $value . "\">\n";
}
echo "</form>";
}
...and so on and so on, each new "page" beginning a new "form" with a different name, with a hidden field using "foreach" to grab all values from the previous pages' fields.
This goes on until the page 5, which has a submit button to finish the script, and then page 6 (confirmation) has the dump to db script:
}
else if ($pageno == "5")
{
$pageno = "6";
echo "<form name='form6' action='form.php' method='get'>";
foreach($_POST as $key => $value) {
echo "<input type=\"hidden\" name=\"" . $key . "\" value\"" . $value . "\">\n";
}
$link = mysql_connect("$db_host","$db_user","$db_pass") or die( "Unable to connect to database");
mysql_select_db("$db_name",$link) or die( "Unable to select database");
$query="insert into test (name,email,address) values
('".$name."','".$email."','".$address."')";
mysql_query($query) or print ">>> MySQL-Error: ".mysql_errno()." -> ".mysql_error()."\n";;
Any ideas what I am doing wrong? Thanks in advance.