Setting cookies in javascript

Status
Not open for further replies.

Sridhar_Rao

In the zone
I looked all around the web, but could not find an article simple enough (for dummies) on setting and manipulating session cookies.

I have a poll running on PHP/MySQL and simple refreshing the page inserts the same data to the table. I don't want people to post again, so I want a session cookie which would expire (say in 5-10 minutes).

My requirements are as follows:
1. A function would check as the poll loads if the browser is set to accept sessions cookie. The javascript code, the functions and where to place, how to call etc.
2. A sessions cookie is set with expiry for 5 minutes. Code with example and explanation
3. Poll is submitted, if user resubmits or refreshes, a function should disable it and inform that it has been disabled.

I found information on setting, manipulating and reading very confusing. Someone please in as elementary language as possible, explain with examples.
 
OP
Sridhar_Rao

Sridhar_Rao

In the zone
Thanks Rohan, but your reply is not helping me. I have a problem and I need solution; with javascript or PHP.
 
OP
Sridhar_Rao

Sridhar_Rao

In the zone
Thanks Rohan! I know you edited and removed those lines.
I know you have given plenty of helpful replies even before. I need your help!
 
Last edited:

Bandu

Journeyman
I have been using cookies for quite some time now, but am not sure what they are - browser cookies or session cookies :-|

See if my sample code is indeed using session cookies using JavaScript, and if we are on the same page, then we can move ahead with some discussion later.

Code from sridhar_rao_1.htm:
Code:
<HTML>
	<HEAD>
		<TITLE>Sridhar Rao</TITLE>
		<script language='JavaScript'>
		function doCooking()
		{
			//document.SridharsForm.SubmitButton.disabled='true';
			if(cookieExists())
			{
				alert('Stop fooling around, nigga!');
				return false;
			}
			else
			{
				setCookie(5);	// 5 minutes
				alert('Good going, mate!');
			}
			return true;
		}
		function cookieExists()
		{
			if(document.cookie.length > 0)
			{
				var begin = document.cookie.indexOf("SriCookie=");
				if(begin != -1)
				{
					begin += "SriCookie".length + 1;
					var end = document.cookie.indexOf(";", begin);
					if(end == -1)
						end = document.cookie.length;
					if('true' == unescape(document.cookie.substring(begin, end)))
						return true;
				}
			}
			return false;
		}
		function setCookie(expiryMins)
		{
			var ExpireDate = new Date();
			ExpireDate.setTime(ExpireDate.getTime() + (expiryMins * 60 * 1000));
			document.cookie = "SriCookie=true" + ((expiryMins == null) ? "" : "; expires=" + ExpireDate.toGMTString());
		}
		function deleteCookie()
		{
			if(cookieExists())
				document.cookie = "SriCookie=; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
		</script>
	</HEAD>
	<BODY>
	<form name='SridharsForm'>
		Poll. Question#1
			<LI>Blah Blah Blah... Choice 1</LI>
			<LI>Blah Blah Blah... Choice 2</LI>
			<LI>Blah Blah Blah... Choice 3</LI><BR><BR>
		<input type='radio' name='SridharRao1' value='1'/>1<BR>
		<input type='radio' name='SridharRao1' value='2'/>2<BR>
		<input type='radio' name='SridharRao1' value='3'/>3<BR>
		<input type='submit' name='SubmitButton' value='Submit' onClick='return doCooking()'/>
		<input type='button' name='NextPage' value='Next Page ==&gt;' onClick='window.location="sridhar_rao_2.htm"'/>
		<BR><BR><BR><BR><BR><BR><BR><BR>
		<input type='button' name='ClearButton' value='Clear Cookie (just to test)' onClick='deleteCookie()'/>
	</form>
	</BODY>
</HTML>
Code from sridhar_rao_2.htm:
Code:
<HTML>
	<HEAD>
		<TITLE>Sridhar Rao</TITLE>
		<script language='JavaScript'>
		function doNothing()
		{
			return false;
		}
		</script>
	</HEAD>
	<BODY>
	<form name='SridharsForm'>
		Poll. Question#2
			<LI>Dooo da doo da do... Choice 1</LI>
			<LI>Dooo da doo da do... Choice 2</LI>
			<LI>Dooo da doo da do... Choice 3</LI><BR><BR>
		<input type='radio' name='SridharRao2' value='1'/>1<BR>
		<input type='radio' name='SridharRao2' value='2'/>2<BR>
		<input type='radio' name='SridharRao2' value='3'/>3<BR>
		<input type='button' name='PrevButton' value='&lt;== Previous page' onClick='window.location="sridhar_rao_1.htm"'/>
		<input type='submit' name='SubmitButton' value='Submit' onClick='return doNothing()'/>
	</form>
	</BODY>
</HTML>
Copy the above 2 code snippets as sridhar_rao_1.htm and sridhar_rao_2.htm respectively.

Tip: If you uncomment the line document.SridharsForm.SubmitButton.disabled='true'
you might have almost similar result as you expect without using cookies, leaving apart the page refresh option though.
 

victor_rambo

हॉर्न ओके प्लीज़
Thanks Rohan! I know you edited and removed those lines.
I know you have given plenty of helpful replies even before. I need your help!
Yes, I realized it was not kind enough of me to post that.
Anywayz, coming to your problem, its a simple 3 line solution:
On the form submission page:
1. check for a cookie everytime
2. if that cookie exists, return an error saying multiple submission not allowed.
3. if that cookie does not exist allow form submission and set the cookie.

PHP:
if(isset($_COOKIE['allow_form_submission']))
{
//sorry, cant submit multiple times
}
else
{
setcookie('allow_form_submission','no',time()+864000)//set the cookie with a life of 10 days
//proceeed with form submission
}
 
OP
Sridhar_Rao

Sridhar_Rao

In the zone
Thanks Rohan, that is exactly what I was looking for. It is indeed so simple in PHP than in javascript. I have implemented it already. The poll is on response to recent terror attack, please have a look. *www.microrao.com/terror_poll.php
Thanks again.
 
Status
Not open for further replies.
Top Bottom