473,403 Members | 2,071 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,403 software developers and data experts.

Javascript cookie Problem

toxicpaint
Hi there,

I've built a form for peolpe to submit a vote and I thought the best way to restrict people voting twice would be to create a cookie called "voted" and set the value to 1 when they vote.

When the page loads I want it to check for the cookie to see if that person has voted or not.

The script is creating a cookie called "vote" and setting it to 1.

I've attempted to write an if statement that checks this, but I can't get it to work.. What have I done wrong?

Thanks a lot!

Tom

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" src="cookies.js">
  2.  
  3.             function checkForVote(){
  4.             if (getCookie("voted") == "1"){
  5.             voteform.submitvote.disabled = true;
  6.             }
  7.             }
  8.             checkForVote();
  9.             </script>
  10. <form name="voteform" action="http://www.liv.ac.uk/cgi-bin/mailcomments.cgi" method="post">
  11. <input name="mailto" type="hidden" value="tcowling@liv.ac.uk">
  12. <input name="subject" type="hidden" value="Someone's Voted">
  13. <input name="required" type="hidden" value="email">
  14. <input name="email" type="hidden" value="someone@acomputer.com">
  15.               <div align="center">
  16.                 <select name="select">
  17.                   <option>Candidate 1</option>
  18.                   <option>Candidate 2</option>
  19.                 </select>
  20.                 <br />
  21.                 <br />
  22.                 <input type="button" value="Submit Vote" onclick='setCookie("voted", "1")' name="submitvote" />
  23.               </div>
  24.             </form>
  25.  
Nov 22 '06 #1
4 1960
I don't know for sure, but try this:
function checkForVote(){
if (getCookie("voted") == "1"){
document.voteform.submitvote.disabled = true;
}
}
notice the "document." If that doesn't work put an alert("hi"); instead of document.voteform...... to see if it ever checks the cookie properly. If neither of those are the issue, physically check to see if the cookie is being created (probably your temp. internet files folder).

you could also try (heaven forbid any of the above don't work):
function checkForVote(){
test = getCookie("voted");
alert(test);
}

Cheers! And good luck!
-Kevin
Nov 22 '06 #2
Thanks for your help Kevin.

None of the above work.

The cookie is definetly being created because I'm using firefox and I've got the web developer toolbar that lets you see what cookies are being created on a page.

Am I calling the function right? Like.. As the page loads should it get to

checkForVote();

and then execute the if statement?

Cheers,

Tom
Nov 22 '06 #3
In your code I don't think you're calling the function checkForVote() once the page loads, so it's never actually run.

I created an example that I think resembles what you're trying to do. Once the person votes, the Vote Button is disabled. I got the code for setCookie() and getCookie() functions from Peter Paul-Koch at http://www.quirksmode.org/js/cookies.html.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Vote Button</title>
  4. <script type="text/javascript">
  5. function checkForVote()
  6. {
  7.     // If there is a cookie...
  8.     if(document.cookie != "")
  9.     {
  10.         if(getCookie("voted") == 1)
  11.         {
  12.             // Disable the button again if user already voted. 
  13.             disableButton();
  14.         }
  15.     }
  16. }
  17.  
  18. function disableButton()
  19. {
  20.     var submitvote = document.getElementById("submitvote");
  21.     submitvote.disabled = true;
  22.     submitvote.value = "You Already Voted.";
  23. }
  24.  
  25. function setCookie(name, value, days)
  26. {
  27.     if(days) 
  28.     {
  29.         var date = new Date();
  30.         date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  31.         var expires = "; expires = " + date.toGMTString();
  32.     }
  33.  
  34.     else 
  35.     {
  36.         var expires = "";
  37.     }
  38.  
  39.     document.cookie = name + "=" + value + expires + "; path=/";
  40.  
  41.     // Disable the button as soon as the user clicks it.
  42.     disableButton();
  43. }
  44.  
  45. function getCookie(name) 
  46. {
  47.     var nameEQ = name + "=";
  48.     var ca = document.cookie.split(';');
  49.  
  50.     for(var i=0; i < ca.length; i++) 
  51.     {
  52.         var c = ca[i];
  53.  
  54.         while (c.charAt(0)==' ') 
  55.         {
  56.             c = c.substring(1,c.length);
  57.         }
  58.  
  59.         if (c.indexOf(nameEQ) == 0) 
  60.         {
  61.             return c.substring(nameEQ.length,c.length);
  62.         }
  63.     }
  64.  
  65.     return null;
  66. }
  67.  
  68. </script>
  69. </head>
  70.  
  71. <body>
  72. <form name="voteform" id="voteform" action="" method="post">
  73. <input type="button" name="submitvote" id="submitvote" onclick="setCookie('voted', 1, 365);" value="Submit Vote"/>
  74. </form>
  75. </body>
  76. <script type="text/javascript">
  77. // Run checkForVote() at end of document so that form and contents can be referenced
  78. window.onload = checkForVote();
  79. </script>
  80. </html>
  81.  
You can just copy & paste entire code to see it work. Hope it helps!
Nov 22 '06 #5

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

Similar topics

0
by: Gowhera Hussain | last post by:
Use This for Learning Only .... Do Not Try To Act Smart HACKING WITH JAVASCRIPT Dr_aMado Sun, 11 Apr 2004 16:40:13 UTC This tutorial is an overview of how javascript can be used to bypass...
2
by: Michael | last post by:
I am reading and setting a cookie using JavaScript in the BODY onload and onunload events respectively. This works fine. However when I use ASP to set the cookie under some condition where I...
3
by: M Wells | last post by:
Hi All, Just wondering how you go about changing the value of a session cookie via javascript? I have a PHP page that sets a session cookie when it first loads. I'd like to be able to change...
13
by: Craig | last post by:
Hey all, Here's the situation: - two websites, one on domain1 and the other on domain2 - domain1 opens a new window which is a javascript app from domain2 - domain1 needs to communicate with...
7
by: Bert | last post by:
I have been reading the post and the FAQ and have been unable to find anything that will help with my problem. First let me say that I am not a web developer, designer and no next to nothing...
7
by: Steph | last post by:
Bonjour, Je souhaite lancer une redirection vers un fichier php via SRC= dans une condition if (voir ci-dessous en bas du script) mais la redirection ne fonctionne pas. Par contre la condition...
1
by: Joe | last post by:
Hi folks, Been looking at this for the day so any ideas would be appreciated. Here's the scenario, bear with me - it looks more complicated than it is :) I have a page templating system that...
3
by: Wysiwyg | last post by:
After a server created cookie is processed on the client I want it removed, cleared, or expired in the javascript block but have been unable to do this. If I set a cookie value in the server code...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.