473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Bad Cookies

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
1 1132
acoder
16,027 Expert Mod 8TB
You're not setting an expiry date for cookies. See this link.
Jan 18 '08 #2

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

Similar topics

4
by: Brian Burgess | last post by:
Hi all, Anyone know of any special issues with storing cookies with ASP? I'm trying this with two browsers: One is IE 6.0 with cookies set to 'prompt'. This has been working properly as any...
20
by: Brian Burgess | last post by:
Hi all, Anyone know if this is possible? If so, on which page would the cookie be? .. On the page calling a function defined in the include file? thanks in advance.. -BB
9
by: | last post by:
Is it possible for a user to enable permanent cookies but disable session cookies.....this seems like a contradition yet this is what I appear to be reading in online articles?
1
by: John Taylor-Johnston | last post by:
I'm a University academic looking for a proper definition of JavaScript Cookies. http://www.CollegeSherbrooke.qc.ca/languesmodernes/604-HAE_Grammar_Practice/ I'm trying to decipher what...
6
by: Mark | last post by:
Hi... I've come across some weird bug with Response.Cookies. Or maybe it will be called "by design" but for the life of me I can't figure out what purpose it would serve. If you're setting a...
8
by: CDARS | last post by:
Hi all, I have a confusing question on ASP.NET cookies usage: 1> Response.Cookies("test").value = Now 2> Response.Write(Request.Cookies("test").value) 3> 4> Response.write("<hr>") 5>...
6
by: Stephane | last post by:
Hi, I have a login page where if the user wants his access codes to be saved are set into a cookie. In the logout page, I want to delete those cookies. I tried this and this is not working at...
1
by: Alex Nitulescu | last post by:
I have the following very simple colde (while learning about cookies and session state): Private Sub cmdAddCookie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
0
by: Claudio | last post by:
I have a demo app where cookies does not work. The first page create a cookies. The second read the contet. If I browse the pages via IE6.0 the pages does not work. If I browse the pages via...
0
by: rn5a | last post by:
This is how I am creating & then reading cookies: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) 'create cookies Response.Cookies("UserName").Value = "Ron"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.