Passing Parameters using Cookies

Question:I would like to pass information in original <input type="text" name="requiredfirst_name"> to second page <input type="text" name="requiredfirst_name">

I have loaded cookie.js in the head of original page and second page, but I have not a clue as to where to put the set_cookie(name, value, expires) or the value = get_cookie(name); so it will save the name and then write the name where I want it in the form.

Your help would be greatly appreciated.
Bruce Brown

Answer: Okay, let's consider this with a simple form with one mandatory input field, a next page button and a clear button. We need to consider the code for the form so that we can match up the names between the form and the Javascript and to see how the Javascript is called. The form is coded as follows:

<form name="myform"><div class="centre">
Your First Name <input type="text" name="requiredfirstname" value="" size="25" maxlength="40" /><br />&nbsp;<br />
<input type="button" name="nextpage" value="Next Page" onclick="xfer(this.form)" onkeyup="xfer(this.form)" /> <input type="reset" name="reset" value="Clear" /></div></form>

Next, in addition to the cookie.js Javascript, we also place the following Javascript in the <head> section of our page:

function xfer(form) {
// validations
if (form.requiredfirstname.value == "") {
alert('Please enter your name');
return false;
var expdate = new Date(); expdate.setTime(expdate.getTime() + 60000);
set_cookie('pass', form.requiredfirstname.value, expdate);

This code is called when the Next Page button is pressed and after validating that you actually entered something into the first name field it stores that value into a cookie (set to expire after one minute) and then transfers to the next page.

There is a copy of this actual form below. Enter your name and press Next Page and we'll go onto the next page where I will show you the code to retrieve the value from the cookie.

Your First Name


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow