View Single Post
Old 09-18-2012, 11:02 PM   PM User | #1
deebo41
New to the CF scene

 
Join Date: Sep 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
deebo41 is an unknown quantity at this point
help with setting/reading/getting cookies

I don't expect someone to do my classwork for me! I would like some help because I don't know where i'm going wrong.This code has been adapted from my previous weeks homework and what it is supposed to do is: Modify the prototype form page so that when the Javascript function has verified that all of the required fields have been filled, a cookie is added to the user's computer. If the same user attempts to fill out the form a second time, the user will be directed to a separate HTML page advising them that they have already submitted the form.




Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta content="en-us" http-equiv="Content-Language" >
<script language="JavaScript" type="text/javascript">
<script>
//Begin
function resetform() {
document.forms[0].elements[1]=="";
}
function submitForms() {
if (isEmail() && isFname() && isLname() && isAddress() && isCity() && isState() && isZip())
if (confirm("\n Press submit to enter your information"))
{
alert("\nYour submission will now be sent!");
return true;
}
else
{
alert("\n You have chosen to abort the submission.");
return false
}
else 
return false;
}
function isEmail() {
if (document.forms[0].elements[1].value == "") {
alert ("\n The E-Mail field is blank. \n\n Please enter your E-Mail address.")
document.forms[0].elements[1].focus();
return false;
}
if (document.forms[0].elements[1].value.indexOf ('@',0) == -1 ||
document.forms[0].elements[1].value.indexOf ('.',0) == -1) {
alert ("\n The E-Mail field requires a \"@\" and a \".\"be used. \n\nPlease re-enter your E-Mail address.")
document.forms[0].elements[1].select();
document.forms[0].elements[1].focus();
return false;
}
return true;
}
function isFname() {
if (document.forms[0].elements[2].value == "")
{
alert ("\n The First Name field is blank. \n\n Please enter your first name.")
document.forms[0].elements[2].focus();
return false;
}
return true;
}
function isLname() {
if (document.forms[0].elements[3].value == "") {
alert ("\n The Last Name field is blank. \n\nPlease enter your last name.")
document.forms[0].elements[3].focus();
return false;
}
return true;
}
function isAddress() {
if (document.forms[0].elements[4].value == "") {
alert ("\n The Address field is blank. \n\nPlease enter your address.")
document.forms[0].elements[4].focus();
return false;
}
return true;
}
function isCity()
{
if (document.forms[0].elements[5].value == "")
{
alert ("\n The City field is blank. \n\nPlease enter your city.")
document.forms[0].elements[5].focus();
return false;
}
return true;
}
function isState() {
if (document.forms[0].elements[6].value == "") {
alert ("\n The state field is blank.\n\nPlease enter your state.")
document.forms[0].elements[6].focus();
return false;
}
return true;
}
function isZip() {
if (document.forms[0].elements[7].value == "") {
alert ("\n The Zip code field is blank. \n\nPlease enter your Zip code.")
document.forms[0].elements[7].focus();
return false;
	}
}
  //acts as flag for redirecting page
var count=0;
//function to check if the form has been filled by same person, by matching the e-mail

function validate(forms) 
{

           //number of days for which the cookie is valid
	var days=99;
	var obj=isEmail;
	var expires = new Date ();
      expires.setTime(expires.getTime() + days * (24 * 60 * 60 * 1000)); 
	
	if(obj.value==readCookie())
		{
		alert("Submission not allowed, this person has already filled form previously.The e-mail address is same!!");
		count=0;

		}
	else
		{
		SetCookie(obj.value,expires);	
		//alert(readCookie());
		count++;

		}
		

  
}
function eraseCookie(name) 
{
	 SetCookie( "", -1);
 }

//function for reading cookie
function ReadCookie()
 {
	 var ca = document.cookie.split(';');
	 var nameEQ = "EmailSubmission" + "=";
	 for(var i=0; i < ca.length; i++)
		 { 
		var c = ca[i]; 
		while (c.charAt(0)==' ')
			 c = c.substring(1, c.length);
		 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
		 } 
	  return null;
 }



//set the cookie

function SetCookie(cookieData, expireDate) 
{
    document.cookie = "EmailSubmission" + "=" + escape(cookieData) + "; expires=" + expireDate.toGMTString();
}    

function GetCookie(name) 
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return GetCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    return null;
}

//this function checks flag and redirects accordingly
function redirect() 
	{
	if(!count)
		location.href = 'error.html';
	else
		location.href = 'acknowledgement.html';
	
	
 	} 
return true;
}
// End
</script>

Last edited by deebo41; 09-19-2012 at 02:47 AM.. Reason: edit for tag enclosement
deebo41 is offline   Reply With Quote