Multi-Page Forms with JavaScript and Cookies
Sometimes, a multi-page form is just better than a one-page
form:
-
Depending on the form's purpose and site visitor
demographics, it may be desirable to present many
smaller forms, in succession, than one very large
form.
-
For some forms, an answer in the current form
determines which form page is presented next.
-
Smaller, multi-page forms may be more aesthetically
pleasing than one large form.
If you will be using Master Form V4.
as your form processor, the software has multi-page form functionality built in. You won't need this article.
If your form processing software does not have multi-page form capability, this article shows you how to carry the information from one form page to another using JavaScript and cookies.
How It Works
When the first form page is submitted, JavaScript stores the form field values in a cookie. The JavaScript then redirects the browser to the second form page.
At the second form page, JavaScript retrieves the values from a cookie and writes hidden fields into the second page form. The field values from the first form are now stored in the second form.
When the second form page is submitted, a cookie stores the values, the browser is redirected, and the third form receives hidden fields with the values retrieved from the cookie.
And so forth, until the last of the multi-page form is loaded into the browser.
The limits to how many forms you can string together are (1) the amount of information carried from form to form (the amount of information that can be stored in a cookie is four kilobytes) and (2) the form user's patience.
Only the last of the multi-page forms is submitted to the normal form handling software. And that submission includes the information carried over from previous forms.
Making It Work For You
It is suggested that you create your multi-page form first. Once all form pages are created, come back and do these steps.
There are instructions for the first form page and for the last form page. If your form is more than two pages, you'll find instructions to apply to each of the middle pages.
Last, there is a large block of JavaScript code to insert into each form page. You'll find instructions for customizing two places in the JavaScript. (The ezine version of this article has a link where the JavaScript can be obtained.)
Okay, here we go.
The First Form Page
The form tag of the first form page has a name attribute and
an onsubmit attribute no action attribute and no
method attribute.
The name attribute specifies the name of the form. (The form
name is also specified in the JavaScript, see below.)
The onsubmit attribute returns the value of the JavaScript
GoToURL() function, which also specifies the URL of the next
form page. Here is an example:
The Last Form Page
The last form page should have an action attribute and a method attribute, their values what would normally be used in a one-page form. It should *not* contain the onsubmit attribute with the GoToURL() function.
In the web page source code, somewhere below the <form...> tag and above the closing </form> tag, put this Java:
Where you put the JavaScript is where the information from the previous form page(s) will be inserted. The information is inserted into the form with hidden fields. It is then part of the current form.
Any Middle Form Pages
If the form has three or more pages, the pages between the first and the last need to have a <form...> tag like the first page and the three lines of JavaScript like the last
page.
ALL Form Pages
All form pages need to have the main JavaScript code. It's 120+ lines.
Put the Javascript somewhere above each of the forms.
In the JavaScript, you'll see two places to customize.
-
The form name. Between the quotation marks, put the
name of the form (the "MyForm" in name="MyForm").
The name may vary from form to form.
-
The cookie name. If you want the cookie name to be
something other than MultiPageFormCookie, specify
the preferred name between the quotation marks.
-
The cookie name must be identical for each page of
the multi-page form.
Every page of the multi-page form needs to have this
JavaScript. Paste it somewhere above the form.
Notes
To use these JavaScript and cookie multi-page forms, the
browser must of course be JavaScript-enabled and allow
cookies to be set.
If your form is too important to leave those details to
chance, consider using Master Form V4. The
software is quite sophisticated, with features including the
ability to add form information to databases. It also has
anti-hijacking code and the ability to block automatic form
submissions (also known as "form spam").
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]
Will Bontrager
©Copyright 2007 Bontrager Connection, LLC Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.