View Full Version : Autofill form text field with a cookie variable

11-11-2004, 10:41 AM
Hi Guys,

Here's hoping you can help....

I have the following code - not elegant but it works to a point (you may notice I'm a new comer after reading!) I have the cookie written but need to autofill the text fields with the data present in the cookie. How do you call the variable? Does it go in the HTML input tag value parameter? When I do this it just gives me the literal text:


How do I get it so it shows "Mr" (if that was the input)?

The code:


<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<title>Form Output</title>

<script language="JavaScript">
var cookie_stuff = unescape(document.cookie);
var crumbs = cookie_stuff.split("|");
var Title = crumbs[0];
var First = crumbs[1];
var Last = crumbs[2];
var Position = crumbs[3];
var Company = crumbs[4];
var Telephone = crumbs[5];
var Email = crumbs[6];
function process(){
var Title = document.Form1.Title.value;
var First = document.Form1.First.value;
var Last = document.Form1.Last.value;
var Position = document.Form1.Position.value;
var Company = document.Form1.Company.value;
var Telephone = document.Form1.Telephone.value;
var Email = document.Form1.Email.value;
var stuff_for_cookie = Title + "|" + First + "|" + Last + "|" + Position + "|" + Company + "|" + Telephone + "|" + Email;

var now = new Date();
now.setTime(now.getTime() + 1000 * 60 * 60 * 24)

document.cookie = escape(stuff_for_cookie)+ ";expires=" + now;

<body bgcolor="#ffffff">
<form action="(Empty Reference!)" method="get" name="Form1">
<input type="text" name="Title" size="50" value="<script>document.write(Title)</script>">Title
<p><input type="text" name="First" size="50">First</p>
<p><input type="text" name="Last" size="50">Last</p>
<p><input type="text" name="Position" size="50">Position</p>
<p><input type="text" name="Company" size="50">Company</p>
<p><input type="text" name="Telephone" size="50">Telephone</p>
<p><input type="text" name="Email" size="50">Email</p>
<p><input type="submit" name="Submit" value="Submit" onClick="process()"></p>


I also need to get rid of the undefined text if there is no cookie.

Can you please help?

Kind regards,

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum