Creating a multi-page order form using PHP

Forms are part of virtually any web application today. They are the main method to receive input from people using the application. They range in size from one-field opt-in forms where you only enter your email address, to very long forms with tens or even hundreds of fields.

To make long forms user-friendlier, it is a good idea to span the form on multiple pages. This can make it easier to follow for the user, and we can also split the data in separate sections, based on the scope (for example separate personal customer information from payment data in a shopping cart checkout form).

One of the challenges that arise from splitting the form over multiple pages is passing the data from one page to another, as at the final point of the form, we have all the needed data ready for processing. We are going to look at two methods to do this: session variables and hidden input fields.

What are sessions anyway?

A HTML session is a collection of variables that keeps its state as the user navigates the pages of a certain site. It will only be available to that domain that created it, and will be deleted soon after the user left the site or closed his browser.

So, the session has a semi-permanent nature, and it can be used to pass variables along different pages on which the visitor lands during a visit to the site.

Multi-page form using sessions

In our example, we are going to create a three pages form, resembling a membership signup and payment form. The first page will ask for customer’s name and address, on the second page there is the choice for membership type, and on the third and final page, payment data must be entered. Final step is saving the data in MySQL.

The first file we are going to create (step 1 of the form) will contain just a simple form with two fields.

<form method="post" action="form2.php">
    <input type="text" name="name">
    <input type="text" name="email_address">
    <input type="submit" value="Go To Step 2">

Ok, so nothing more than 2 input fields and a submit button to take us to step 2. In the following page, apart from the HTML form to gather membership data, we are going to need code to store the submitted data from step 1 in the session.


//let's start the session

//now, let's register our session variables

//finally, let's store our posted values in the session variables
$_SESSION['name'] = $_POST['name'];
$_SESSION['email_address'] = $_POST['email_address'];

<form method="post" action="form3.php">
<input type="radio" name="membership_type" value="Free">
<input type="radio" name="membership_type" value="Normal">
<input type="radio" name="membership_type" value="Deluxe">
<input type="checkbox" name="terms_and_conditions">
<input type="submit" value="Go To Step 3">

On to step 3, we must again create new session variables, assign values received by post, and create the final part of the form.


//let's start the session

//now, let's register our session variables

//finally, let's store our posted values in the session variables
$_SESSION['membership_type'] = $_POST['membership_type'];
$_SESSION['terms_and_conditions'] = $_POST['terms_and_conditions'];

<form method="post" action="form_process.php">
<input type="text" name="name_on_card">
<input type="text" name="credit_card_number">
<input type="text" name="credit_card_expiration_date">
<input type="submit" value="Finish">

Now that we created step 3 of the form, what’s left is the final processing script that inserts the data in the MySQL database.


//let's start our session, so we have access to stored data

//let's create the query
$insert_query = 'insert into subscriptions (
						) values (
				" . $_SESSION['name'] . ",
				" . $_SESSION['email_address'] . ",
				" . $_SESSION['membership_type'] . ",
				" . $_SESSION['terms_and_conditions'] . ",
				" . $_POST['name_on_card'] . ",
				" . $_POST['credit_card_number'] . ",
				" . $_POST['credit_card_expiration_date'] . "

//let's run the query

And we are done. Please note that in the final query, we are using data from the $_SESSION array, and also data from the $_POST array, that was posted from the last step of the form.

Comments on this entry are closed.

  • Thanks a million. This will help me on my next php project

  • Thank a lot dude. Really nice post.

  • Great post! I do have 2 questions because im new to this stuff.
    1. How or what code do I use to send the form data. I need the data to be sent to my email address?
    2. What would be the code to connect to the database to record the user data?
    Is it possible that you can extend your post to cover the above questions?

    Thank you very much 🙂

    • Hi Jeremy,

      did you work out your email sending part?

      let me know, because i can give you some snippets.

      • Hi John. I would like to get the email snippets if you have them available. I think this is a great form, you have done a good job. I just need to be able to send all info at one time to an email. Any help would be great



      • Amazing, and my humble thanks for the Post 🙂

        In the last Form, I want to add four input fields to enter valid email address. And send the the required data to the all four entered email address.

        I need your assistance with coding.

        Thank you Again Dear 🙂

      • Please send me some snippets to send the complete multi-part form via email from the saved sessions (all data)… instead of posting to a database.

        Thanks a million!

        • Great post!

          Could I also please get some snippets for the multi-page form via email instead of posting to the database.

          Many thanks!

  • great job my friend thanks a lot !!!!

  • This is a nice and clear example, but you really should mention data sanitation in a post like this: SQL injection attacks can totally destroy a website, so any time you have a web form, you MUST validate and sanitize your data!
    To comically illustrate the point:

  • oww nice and very easy coding.I am new php coder and clear now about session.

  • Thanks for this very clear and precise example. Just what I was looking for to get a clear idea on how the session variables work!!

  • Thanks..

  • Neat. The first page I’ve found in which explains all relevant information and is concise at the same time. Most people out there seem to have learnt things in such a mechanical way that they seem not to really know what they’re doing. They just memorized series of steps without knowing what they meant, and then dare to make bizarre tutorials in which they elaborate on irrelevant details while major information passes unexplained, and all with clumsy examples. I’ve never had a hard time to learn anything I wanted, but in the internet even the stupidest subject becomes a monster when people who seem not to know even what it means to ‘understand’ something try to explain it.
    You should consider extending your area of subjects. Your articles are far better than any other on the internet.

  • How many files do I need to create using the above and what should they be called ?

  • Thanks dear

  • Hi,

    Can explain me the following if its possible using only ONE Single button to do the multiple task at a time?

    One button can save the the lengthy profile information’s and than than take up credit card information’s and pass it through payment gateway Moneris ?

    We have tried using session, but its not working out.

    Let me know if anyone can help me out with this.

  • Thanks a ton mate.

    I’m going to use this concept for my next jQuery Mobile project.

    Will update you, if I find any difficulties.

  • Nice article!
    Question: How many forms shall i use if i have one checkbox with 3 buttons (add, delete, edit) ?

  • wow, this is really useful.
    But, how do I eliminate MySql injection possibilities?

  • i got this code so its good to work i can apply now with multipages session variables.


  • how can i can update an account…situation when he pass the filter form that he is an alumni then after that he creates an account that update to his record…code pls


  • Do I need the session_start() in the beginning of every page? If yes, why? What will happen if I do not use it/miss it in the subsequent pages?

    Consider a scenario where field survey questionnaire is being entered into an application (muli-page of couse). At the end of first set, is there a simple way to flush all/most of the session variables and start over?

  • Hey buddy , thanks for the post ,you save my day!!!!