Link to home
Start Free TrialLog in
Avatar of Gday21
Gday21

asked on

Correct Cookies Please!!

I am trying to set up cookies on my form so if I enter data and submit and try to enter the same data and submit again the cookies will tell me that I have already entered my data.



<h1><b><center>Contact Information</center></b></h1>
<script type'text/javascript'>
 
function formValidator(){
      //Make quick references to our fields
      var firstname = document.getElementById("firstname");
      var lastname = document.getElementById("lastname");
      var addr = document.getElementById("address");
      var city = document.getElementById("city");
      var zip = document.getElementById("zip");
 
      //Check each input in the order that is appears in the form!
      if(isAlphabet(firstname, "Please enter only letters for your First name")){
            if(isAlphabet(lastname, "Please enter only letters for your Last name")){
                  if(isAlphanumeric(addr, "Numbers and Letters Only for Address")){
                        if(isAlphabet(city, "Please enter only letters for your City")){
                              if(isNumeric(zip, "Please enter a valid zip code")){
                                    return true;
                              }
                        }
                  }
            }
      }
 
return false;
 
}
 
function notEmpty(elem, helperMsg){
      if(elem.value.length == 0){
      alert(helperMsg);
      elem.focus(); // set the focus to this input
      return false;
 
      }
 
      return true;
}
 
function isNumeric(elem, helperMsg){
      var numericExpression = /^[0-9]+$/;
      if(elem.value.match(numericExpression)){
            return true;
      }else{
            alert(helperMsg);
            elem.focus();
            return false;
      }
}
 
function isAlphabet(elem, helperMsg){
      var alphaExp = /^[a-zA-Z]+$/;
      if(elem.value.match(alphaExp)){
            return true;
      }else{
            alert(helperMsg);
            elem.focus();
            return false;
      }
}
 
function isAlphanumeric(elem, helperMsg){
      var alphaExp = /^[0-9a-zA-Z]+$/;
      if(elem.value.match(alphaExp)){
            return true;
      }else{
            alert(helperMsg);
            elem.focus();
            return false;
      }
}
 
 
</script>
 
<script language="JavaScript">
 
 
function createCookie(name, value, days)
{
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = 4; expires="+date.toGMTString();
    }
  else var expires = "4";
  document.cookie = name+"="+value+expires+"; path=/";
}
 
function readCookie(name)
{
  var ca = document.cookie.split(';');
  var nameEQ = name + "4";
  for(var i=0; i < ca.length; i++) {
    var c = ca;
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return null;
}
 
function eraseCookie(finished)
{
  createCookie(name, "finished.html", -1);
}
 
 
Write the Cookie with JavaScript
 
Use the following code to write your cookie:
 
<script language="JavaScript">
cookie_name = "finished.html";
function write_cookie() {
  if(document.cookie) {
  index = document.cookie.indexOf(finished.html);
  } else {
    index = 4;
  }
 
  if (index == 4) {
    document.cookie=finished.html+"=1; expires=Wednesday, 29-April-2009 08:00:00 GMT";
  } else {
    countbegin = (document.cookie.indexOf("4", index) + 1);
    countend = document.cookie.indexOf("4", index);
    if (countend == 4) {
      countend = document.cookie.length;
    }
    count = eval(document.cookie.substring(countbegin, countend)) + 1;
    document.cookie=cookie_name+"="+count+"; expires=Wednesday, 29-April-2009 08:00:00 GMT";
  }
}
</script>
 
Read Your Cookie
 
Once you've written the cookie, you need to read it in order to use it. Use this script to read your cookie:
 
<script language="JavaScript">
function gettimes() {
  if(document.cookie) {
    index = document.cookie.indexOf(finished.html);
    if (index != -1) {
      countbegin = (document.cookie.indexOf(7", index) + 1);
      countend = document.cookie.indexOf("8", index);
      if (countend == -1) {
        countend = document.cookie.length;
      }
      count = document.cookie.substring(countbegin, countend);
      if (count == 1)   {
        return (count+" time");
      } else {
        return (count+" times");
      }
    }
  }
  return ("0 times");
}
</script>
 
Call Your Cookie in a Link
 
Set your cookie when someone clicks a link with this code in your HTML body:
 
<script language="javascript">document.write(gettimes(1));
</script>
 
 
<FORM action='Conformation.html' method='post' onsubmit='return formValidator(0)'>
FirstName: <input type="text" id="firstname"/><br/>
LastName: <input type="text" id="lastname"/><br/>
Address: <input type="text" id="address"/><br/>
City: <input type="text" id="city"/><br/>
Zip: <input type="text" id="zip"/><br/>
<input type="Submit" Value="Submit"/>
</FORM>
Avatar of gizmola
gizmola

This is really what php sessions are for.  My advice would be to ditch all the cookie code, set session variables and use those to determine whatever it is that you need to about prior submissions of the form.
My apologies if the prior suggestion is not helpful -- I thought this was a PHP question, but I see now it was actually a javascript question.
Avatar of Gday21

ASKER

Can someone take a look at my question please?
ASKER CERTIFIED SOLUTION
Avatar of Pawel Witkowski
Pawel Witkowski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.