By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,781 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,781 IT Pros & Developers. It's quick & easy.

Bad Cookies

P: 1
I am very new to Javascript, and I am trying to create that will create a cookie that will store the user first and last name from a form. The entre has to be validate first and then the cookie is saved. If the user writes his name again it prompts him that they have been to the page before. If a new user enter their name then it writes over the old data. Right now when I press submit the code does not do anything.

Here is the code:

[HTML] <html>
<head>
<script type="text/Javascript">
[/HTML]
Expand|Select|Wrap|Line Numbers
  1.     function validate() {
  2.        var fname = String(document.myForm.Fname.value);
  3.        var lname = String(document.myForm.Lname.value);
  4.        var email = String(document.myForm.myEmail.value);
  5.        var username = Fname +""+ Lname;
  6.        var msg = "Thank you for visiting again.";
  7.        var err = false;
  8. if ((fname.length == 0)||(lname.length == 0)||(email.length==0)){
  9.       msg = "Please enter information in all three fields";
  10.       err = true;
  11. }
  12. if (err == false) {
  13.    if (email.indexOf("@") == -1) {
  14.        msg = "Your email is missing a @.";
  15.        err = true;
  16.    }
  17. }
  18. if (err == false) {
  19.    if (email.indexOf(".") == -1) {
  20.        msg = "Your email address is missing a period.";
  21.        err = true;
  22.    }
  23. }
  24.  
  25. if (msg != "")
  26.   {var myWindow = window.open("","","width=400,height=400");
  27.    myWindow.document.write(msg);}
  28.  
  29. if (msg == "" && err == ""){
  30.   if(getCookie('chips') == null)
  31.    {var expdate = new Date();
  32.  
  33.  expdate.setTime(expdate.getTime() + (1000 * 60* 60 *24 *365));
  34.     setCookie('chips',username);
  35.     window.open() }
  36.  
  37. if((getCookie('chips') != null) && (getCookie('chips') == username))
  38.    {msg = msg + username + "You entered your name already!\n"
  39.     var myWindow = window.open("","","width=400,height=400");
  40.     myWindow.document.write(msg);}
  41.   else
  42.     {var expdate = new Date()
  43.  
  44.     expdate.setTime(expdate.getTime());
  45.      setCookie('chips',username);
  46.      window.open()}
  47.  
  48.      return(msg)
  49.      }
  50. function getCookieVal (offset){
  51.  var cooklen = document.cookie.indexOf (";", offset);
  52.  
  53.  if (cooklen == -1)
  54.   {cooklen = document.cookie.length;}
  55.   return unescape(document.cookie.substring(offset, cooklen));
  56.   }
  57.  
  58.   function getCookie (c_name){
  59.  var arg = c_name + "=";
  60.  var argLen = arg.length;
  61.  var cooklen = document.cookie.length;
  62.  var i = 0;
  63.  
  64.  while (i < cooklen)
  65.   {var j = i + argLen;
  66.  
  67.    if (document.cookie.substring(i, j) == arg)
  68.    {return getCookieVal(j)}
  69.    if (i == 0)
  70.    {break}
  71.   }
  72.  return null;
  73.   }
  74.  
  75.   function setCookie(name, value){
  76.  var argv = setCookie.arguments;
  77.  var argc = setCookie.arguments.length;
  78.  var expires = null;
  79.  var path = null;
  80.  var domain = null;
  81.  var secure = null;
  82.  
  83.  document.cookie = name + "=" + escape(value) +
  84.   ((expires == null) ? "" : ("; expires=" + expires.toGTMString())) +
  85.   ((path == null)  ? "" : ("; path=" + path)) +
  86.   ((domain == null)  ? "" : ("; domain=" + domain)) +
  87.   ((secure == true)  ? "; secure" : "");
  88.   }
  89.  
  90. /*  function deleteCookie () {
  91.  var expired = new Date();
  92.  expired.setTime (expired.getTime() - 1000000000);
  93.  
  94.  var cookieValue = getCookie ('chips');
  95.  
  96.  document.cookie = 'chips' + "=" + cookieValue + ";
  97.   expires=" + expired.toGMTString();
  98.   }
  99.  
  100. // Create a new window
  101. var myWindow = window.open("","","width=400,height=400");
  102. myWindow.document.write(msg);
  103. }
[HTML]</script>
</head>
<body>
<form name="myForm">
First Name: <input name="Fname" type="text"><br>
Last Name: <input name="Lname" type="text"><br>
Email:<input name="myEmail" type="text"><br>
<input type="button" value="submit" onclick="validate()">
</form>
</body>
</html>[/HTML]
Jan 18 '08 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
You're not setting an expiry date for cookies. See this link.
Jan 18 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.