We help IT Professionals succeed at work.
Get Started

Creating and Reading Cookies

rhivka asked
Last Modified: 2012-08-13
I started out with a form that validates user has entered name and either physical or email address when submitted. Then opens confirm.html saying their form has been submitted. Now after JavaScript function has verified that all of the required fields have been filled, I want to add a cookie to the user's computer. If the same user attempts to fill out the form a second time, the user will be directed to a separate HTML page advising them that they have already submitted the form.

What I have below is not working properly. As soon as the form is loaded the first time, the alert window pops up saying the user has already submitted the form...instead of waiting until the user has filled out the form first and returned to the page.

Also, I need to change the alert window of  function getUserPrefs() to open a Web page such as Return.html....but only if the same user returns to fill out the form a second time. Meaning I want the form to still go to Confirm.html the first time a user fills out the form. I think I created the cookie correctly, and set it to expire after 1 week. Any help would be greatly appreciated.

<script type="text/javascript">
        function validateForm() { /* validates user enters name */
      var a,i,o = document.mailList;
      if(o.name_shipping.value == ""){
         alert("You must enter your name");
         return false;
      if(o.email.value == ""){ /* validates either email or street address is entered */
         a = ["name_shipping","address_shipping","city_shipping","state_shipping","zip_shipping"];
         for (i=0;i<a.length;i++) if(o[a[i]].value == "") {
            alert("You must enter either your street address or email address.");
            return false;
      a = ["area","exchange","phone"]; /* validates user enters phone number */
      for(i=0;i<a.length;i++) if(o[a[i]].value == "") {
         alert("You must enter your telephone number.");
         return false;
      a = ["area","exchange","phone"]; /* validates user inputs numeric values in phone fields */
      for (i=0;i<a.length;i++) if(isNaN(parseInt(o[a[i]].value))) {
         alert("You must enter numeric values!");
         return false;
      return true;
         function setCookie() {
            var expiresDate = new Date();
            cookieDate.setDate(myDate.getDate() + 7);
            document.cookie = encodeURI("name_shipping=" + document.forms[0].name_shipping.value) + "; expires="
      + expiresDate.toUTCString();
         function getUserPrefs() {
      var userPrefs = decodeURI(document.cookie);
      var prefs = userPrefs.split("; ");
      var userName = prefs[0].split("=");
      window.alert("Welcome back " + userName[1] + ". You have already submitted this form. ");

<body onload="getUserPrefs();">
   <h1>Kudler Customer Mailing List</h1>
   <h4>Sign up here to be notified of upcoming sales and events.</h4><hr />
   <form name="mailList" action="confirm.html" enctype="application/x-www-form-urlencoded" onsubmit="return validateForm();">
      <table border="0" cellpadding="3" cellspacing="0">
      <td valign="top">
      <p>Name<br />
      <input type="text" name="name_shipping" size="50" /><br /></p>
      <p>Address<br />
      <input type="text" name="address_shipping" size="50" /></p>
      <p>City, State, Zip<br />
      <input type="text" name="city_shipping" size="34" />
      <input type="text" name="state_shipping" size="2" maxlength="2" />
      <input type="text" name="zip_shipping" size="5" maxlength="5" />
      <p>E-mail<br />
      <input type="text" name="email" size="50" />
      <p>Telephone<br />
      (<input type="text" name="area" size="3" maxlength="3" />)
      <input type="text" name="exchange" size="3" maxlength="3" />
      <input type="text" name="phone" size="4" maxlength="4" />
      <p><input type="submit" value="Submit" onclick="setCookie();" style="width: 150px" /></p>
Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE