469,649 Members | 1,443 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,649 developers. It's quick & easy.

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 1755
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

Post your reply

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

Similar topics

reply views Thread by Gowhera Hussain | last post: by
3 posts views Thread by M Wells | last post: by
7 posts views Thread by Bert | last post: by
7 posts views Thread by Steph | last post: by
1 post views Thread by Joe | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.