We help IT Professionals succeed at work.

HTML Form variables carried from page to page

robthomas09 asked
Hello experts,

I have a 1-page form that I need to turn into 4 pages.  Essentially, it is a very large application form that I have all on one page, and need to break it down into several manageable-sized pages with a "Next" button at the bottom of each page.  

On the final page, ALL form fields from the previous pages need to be collected and submitted:

So, for example on page 1 we have:

City: <input type="text" name="city">
State: <input type="text" name="state">
Zipcode: <input type="text" name="Zipcode">

and on page 2 we have:

Country: <input type="text" name="Country">
Phone: <input type="text" name="phone">

and on page 3 we have:

ShiptoState: <input type="text" name="shiptostate">
shiptozipcode: <input type="text" name="shiptozipcode">

... when you click the submit button on the third page, how do I get the variables from pages one and two to be submitted with the third?  

I thought of creating hidden form fields on the third page, but wasnt sure how to get the value to be the field from the previous pages.


Thanks in advance!
Watch Question

The easiest way would be to use session.
Use sessions
PHP Example:

<head><title>PHP Session example..</title></head>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
country: <input type="text" size="15" name="country"><br />
phone: <input type="text" size="10" name="phone" > <br />
<a href="third_pag.php">Next</a>
$_SESSION['country'] = $_POST['country'];
$_SESSION['phone'] = $_POST['phone'];

and in the third page
$country = $_SESSION['country'];
$phone = $_SESSION['phone'];

Open in new window

Also a php sessions tutorial is available here > http://www.tizag.com/phpT/phpsessions.php


thanks both of you - I'll try it out and get back to you soon!
You are welcome , and i am waiting if you still need any help.
In case you would like to use hidden fields the way to do it is like this, as you know the name of the form field is the name of the variable;

<input type="text" name="email">

when you submit this to the next page you grab the value like so

$email = $_POST['email'];

you can then assign this value to the hidden field like this

<input type="hidden" name="email" value="<?php echo "$email"; ?>">

Just keep it mind though that you need to carry it page after page while with a session it would be stored and be available whenever you need it.

If you want to go through PHP, this request is so basic it is obvious -- when PHP gets the form data, it just echos the values back to the next page as hidden form fields, and when that submits to the next PHP page, it does the same, until in the final page ALL form fields are present in the final HTML page.

But that is a LOT of work.  Why do all that when you can just pass the variables to the next HTML page.  So what do you want to do?  Use PHP or not?  It makes all the difference in how the question is answered.