You are here: Home » All Posts » PHP Form » Creating a multi-page order form using PHP

Creating a multi-page order form using PHP

in PHP Form

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">
</form>

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.

<?php
//let's start the session
session_start();
//now, let's register our session variables
session_register('name');
session_register('email_address');
//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">
</form>

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

<?php
//let's start the session
session_start();
//now, let's register our session variables
session_register('membership_type');
session_register('terms_and_conditions');
//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">
</form>

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.

<?php
//let's start our session, so we have access to stored data
	session_start();
//let's create the query
$insert_query = 'insert into subscriptions (
				name,
				email_address,
				membership_type
				terms_and_conditions,
				name_on_card,
				credit_card_number,
				credit_card_expiration_date
						) 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
mysql_query($insert_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.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

No related posts.

{ 47 comments… read them below or add one }

huntsville September 28, 2011 at 6:10 am

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

Reply

Abdul Baquee September 28, 2011 at 4:42 pm

Thank a lot dude. Really nice post.

Reply

Jeremy September 29, 2011 at 7:06 pm

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 :)

Reply

john December 15, 2011 at 12:21 am

Hi Jeremy,

did you work out your email sending part?

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

Reply

Ali December 21, 2011 at 7:07 am

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

Thanks

Ali

Reply

Berpke December 25, 2011 at 4:24 am

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 :)

Reply

Dan January 26, 2012 at 9:30 pm

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!

Reply

samir October 2, 2011 at 9:24 pm

great job my friend thanks a lot !!!!

Reply

Fireproofsocks October 30, 2011 at 8:17 pm

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:
http://xkcd.com/327/

Reply

childish on fiverr November 21, 2011 at 9:23 pm

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

Reply

Arun November 23, 2011 at 4:27 am

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!!

Reply

newslover December 10, 2011 at 8:22 am

Thanks..

Reply

Gabriel December 20, 2011 at 7:57 pm

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.

Reply

John December 28, 2011 at 8:46 am

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

Reply

zulfi January 6, 2012 at 4:05 am

Thanks dear

Reply

debraj January 6, 2012 at 11:32 am

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.

Reply

Sandeep January 17, 2012 at 6:57 am

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.

Reply

plsql January 18, 2012 at 8:41 am

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

Reply

Brandon Lu January 27, 2012 at 4:42 pm

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

Reply

Leave a Comment

Previous post:

Next post: