Passing HTML form data between web pages using JavaScript Cookies


#1

I am trying to pass form data between 2 web pages using JavaScript and cookies in Dreamweaver 5. The web is filled with examples but non seem to have worked for me. I am back to this one that i found on the web and i want to make it work but its not working. What am I doing wrong ? Could somebody please help am new to cookies and its doing my head in !

This is the page setting the cookie

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Send Data</title>

<script type="text/javascript">

  // Original JavaScript code by Chirp Internet: www.chirp.com.au
  // Please acknowledge use of this code by including this header.

  var today = new Date();
  var expiry = new Date(today.getTime() + 30 * 24 * 3600 * 1000); // plus 30 days

  function setCookie(name, value)
  {
    document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
  }

</script>

<script type="text/javascript">

  function storeValues(form)  
  {
    setCookie("field1", form.field1.value);
    setCookie("field2", form.field2.value);
    setCookie("field3", form.field3.value);
    setCookie("field4", form.field4.value);
    return true;
  }

</script>
</head>
</body>
<form name="form" method="post" action="/javascript/getcookie/" onsubmit="return storeValues(this);">
<fieldset>
<label>Field 1</label><span><input type="text" size="32" name="field1" value=""></span>
<label>Field 2</label><span><input type="text" size="32" name="field2" value=""></span>
<label>Field 3</label><span><input type="text" size="32" name="field3" value=""></span>
<label>Field 4</label><span><input type="text" size="32" name="field4" value=""></span>
<span><input type="submit" value="Set Cookies">
<input type="button" onclick="clearCookies();" value="Clear Cookies"></span>
</fieldset>
</form>
</body>
</html>

This is the page retrieving the data

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Receive Data</title>
<script type="text/javascript">

  if(field1 = getCookie("field1")) document.myForm.field1.value = field1;
  if(field2 = getCookie("field2")) document.myForm.field2.value = field2;
  if(field3 = getCookie("field3")) document.myForm.field3.value = field3;
  if(field4 = getCookie("field4")) document.myForm.field4.value = field4;

</script>
</head>
<body>
<form name="myForm" method="post" action="">
<fieldset>
<label>Field 1</label><span><input type="text" size="32" name="field1" value=""></span>
<label>Field 2</label><span><input type="text" size="32" name="field2" value=""></span>
<label>Field 3</label><span><input type="text" size="32" name="field3" value=""></span>
<label>Field 4</label><span><input type="text" size="32" name="field4" value=""></span>
<span><input type="submit" value="Set Cookies">
<input type="button" onclick="clearCookies();" value="Clear Cookies"></span>
</fieldset>
</form>
</body>
</html>