Adding cookie

Needed to add cookie to allow user to only submit info once

Page 1 of 1

3 Replies - 1603 Views - Last Post: 22 September 2008 - 07:34 PM

#1 juseyo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 22-September 08

Adding cookie

Posted 22 September 2008 - 06:47 PM

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Transitional DTD Template</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript">
<!-- HIDE HEAD CODE FROM INCOMPATIBLE BROWSERS
/*////////////////////////////////////////////////////////////////////////
PROGRAM INFORMATION

Programmer : Julie Dawson
Date	: September 22, 2008
Name	: Kudler_customer_demographic_form
Filename: jdawson_wk4part1.html
Purpose : wk 4 ind ass: complete change request #2

////////////////////////////////////////////////////////////////////////*/
STOP HIDING HEAD CODE FROM INCOMPATIBLE BROWSERS -->
</script>
</head>

<body>
<script type="text/javascript">
<!-- HIDE BODY CODE FROM INCOMPATIBLE BROWSERS

function validateInformation() {
	if (confirmSubmit() == false)
		return false;
	if (cookieInformationExists() == true) {
		window.location = "formAlreadyFilled.html";

	}
	else {
		setCookie();
		return true;
		return false;
	}
	return false;
}
function cookieInformationExists() {
	if (document.cookie != "") {
		splitCookie = document.cookie.split("; ");
		userName = unescape(splitCookie[0].split("=")[1]);
		if (userName == "") {
			return false;
		}
		else {
			if (userName!=kudler.name.value)
				return false;
			return true;
		}
	}
	else {
		return false;
	}
	return false;
}
function setCookie() {
	var expiresDate = new Date();
	expiresDate.setMonth(expiresDate.getMonth() + 6);
	document.cookie = encodeURI("userName="+document.forms[0].name.value)+";expires="+expiresDate.toUTCString();
	document.cookie = encodeURI("userAddress="+document.forms[0].address.value)+";expires="+expiresDate.toUTCString();
	document.cookie = encodeURI("userCity="+document.forms[0].city.value)+";expires="+expiresDate.toUTCString();
	document.cookie = encodeURI("userState="+document.forms[0].state.value)+";expires="+expiresDate.toUTCString();
	document.cookie = encodeURI("userZip="+document.forms[0].zip.value)+";expires="+expiresDate.toUTCString();
	document.cookie = encodeURI("userAreacode="+document.forms[0].areacode.value)+";expires="+expiresDate.toUTCString();
	document.cookie = encodeURI("userExchange="+document.forms[0].exchange.value)+";expires="+expiresDate.toUTCString();
	document.cookie = encodeURI("userPhone="+document.forms[0].phone.value)+";expires="+expiresDate.toUTCString();
	document.cookie = encodeURI("userEmail="+document.forms[0].email.value)+";expires="+expiresDate.toUTCString();

	return true;
}
function checkForNumber(fieldValue) {
	var numberCheck = isNaN(fieldValue);
	if (numberCheck == true) {
		window.alert("You must enter a numeric value");
		return false;
	}
}

function confirmSubmit()
{
	if (document.forms[0].name.value ==""
		||document.forms[0].address.value ==""
		||doument.forms[0].city.value ==""
		||document.forms[0].state.value ==""
		||document.forms[0].zip.value ==""
		||document.forms[0].areacode.value ==""
		||document.forms[0].exchange.value ==""
		||document.forms[0].phone.value ==""
		||document.forms[0].email.value ==""){
		window.alert("You must complete form before submitting.");
		return false;
	}

	return true;
}


// STOP HIDING BODY CODE FROM INCOMPATIBLE BROWSERS -->

</script>
<h1>Kudler Customer Demographic Information</h1>
<h2>Customer Mailing List</h2><hr/>

<form name="kudler" action="kudlerformprocessor.html" method="post" enctype="text/plain"
	onsubmit="return validateInformation(this)">
<p>Name (ex. George Bush)<br/>
	<input type="text" name="name" size="30"/></p>
<p>Address (ex. 1600 Pennsylvania Ave NW)<br/>
	<input type="text" name="address" size="50"/></p>
<p>City, State, Zip (ex. Washington, DC 20500)<br/>
	<input type="text" name="city" size="20"/>
	<input type="text" name="state" size="20"/>
	<input type="text" name="zip" size="7"
	onchange="return checkForNumber(this.value);"/></p>
<p>E-Mail Address (ex. GWB@whitehouse.gov)<br/>
	<input type="text" name="email" size="50"/></p>
<p>Daytime Phone Number (ex. (202) 456-1111)<br/>
	(<input type="text" name="areacode" size="3" maxlength="3"
	onchange="return checkForNumber(this.value);"/>)
	<input type="text" name="exchange" size="3" maxlength="3"
	onchange="return checkForNumber(this.value);"/>
	<input type="text" name="phone" size="4" maxlength="4"
	onchange="return checkForNumber(this.value);"/></p>
<p><input type="submit" value="Submit Info"/>
	<input type="reset"/></p>

</form>
</body>
</html>



Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Adding cookie

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Adding cookie

Posted 22 September 2008 - 07:05 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:

  • Post the code you're having problems with (DONE)
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do

Was This Post Helpful? 0
  • +
  • -

#3 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Adding cookie

Posted 22 September 2008 - 07:09 PM

Just my 2 cents, but javascript cookies aren't the best way to prevent submission:

1) what if javascript is disabled
2) smart users may delete the cookie
3) cookies expire
4) temporary file cleaners often remove cookies

Of course if this is an assignment and you are forced to use this method, these are just things that are good to keep in mind.
Was This Post Helpful? 0
  • +
  • -

#4 juseyo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 22-September 08

Re: Adding cookie

Posted 22 September 2008 - 07:34 PM

Sorry, it might be helpful if I said what was wrong with it. The cookie is not working (I guess) because I can submit the same form with the same info over and over and not get to the "you already filled out this form" message.


View PostPsychoCoder, on 22 Sep, 2008 - 07:05 PM, said:

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with (DONE)
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1