Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 03:47 AM. Reason: edit for tag enclosement

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    <meta content="en-us" http-equiv="Content-Language" >
    <script language="JavaScript" type="text/javascript">
    This is very old code - use of language="JavaScript" has been deprecated for a long time, as has the use of elements[]. I hope that your teacher didn't promote this code to you?

    You also need to enclose your code in [CODE ] and [ /CODE] tags (without spaces).

    Having said all this, you haven't asked a question, or described the error or issue that you are having ??
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem I am having is (1)when a user clicks submit, there is no cookie set.
    (2)Since there is no cookie set, there is no redirection to either a confirmation page labeled acknowledgement.html that would state the users info has been collected or to a page labeled error.html which would tell the user that the information has already been submitted.

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    You have an unmatched bracket:
    Code:
    //this function checks flag and redirects accordingly
    function redirect() 
    {
    if(!count)
    location.href = 'error.html';
    else
    location.href = 'acknowledgement.html';
    
    
    } 
    // DELETE THIS LINE return true;
    // AND THIS LINE }
    I haven't looked beyond this.

    Please edit your OP to wrap the code in [ CODE ] tags.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    I suggest you start by fixing the errors indicated in the console.

  • #6
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    I suggest you start by fixing the errors indicated in the console.
    I have fixed all errors in the console, but I still can't get the cookie set/read, or the redirection to an error page.

  • #7
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by deebo41 View Post
    I have fixed all errors in the console, but I still can't get the cookie set/read, or the redirection to an error page.
    It would help if you would post the code in its current state, including the markup of the form itself.

  • #8
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well thanks to my online teacher, I still haven't recieved a markup for my code even tthough I submiteed it Sunday. But anyway I revised my code and I think I am almost there but I wonder if I am using an undeclared variable or if I incorrectly initiated a function. for some reason the cookie is not being set/read to redirect users to an error page. I am trying to set the cookie value with a persons email address since that is a unique identifier. Thank You in advance for anyones help in pointing out my errors! Here is the code:

    Code:
    //<!--  Begin
    function submitForm() 
    {
        if (document.forms[0].fname.value == "" || document.forms[0].lname.value == "") {
            window.alert("You must enter your first and last names.");
            return false;
        }
        else
        {
            if (document.forms[0].address.value == "" && document.forms[0].email.value == "") 
            {
                window.alert("You must enter either a mailing address or email address.");
                return false;
            }
            else
            {
                return true;
            }
        }
    }
    //acts as flag for redirecting page
    var count=0;
    
    //fuction checks if the form has been filled by same person two times, by matching the e-mail id, as email id is unique for each person
     
    function validate(document) 
    {
     
    //call function for validating, if any field is empty
    	formCheck(document);
    //number of days for which the cookie is valid, here it is set to 99 days, may be changed by changin the value
    	var days=99;
    	var obj=document.forms["email"];
    	var expires = new Date ();
          expires.setTime(expires.getTime() + days * (24 * 60 * 60 * 1000)); 
    	
    	if(obj.value==readCookie())
    		{
    		alert("Submission not allowed, the person has already filled form previously.The e-mail id 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 = "EmailSubmissions" + "=";
    	 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;
     }
     
     
     
    //setting up cookie
     
    function SetCookie(cookieData, expireDate) 
    {
        document.cookie = "EmailSubmissions" + "=" + 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 = 'Confirm.html';
    	
    	
     	} 
    // End -->
    </script>
    <title></title>
    
    <style type="text/css">
     div.c4 {text-align: center}
     table.c3 {background-color: #FFFF66}
     span.c2 {font-family: courier}
     span.c1 {font-family: courier; font-size: 144%}
    .auto-style1 {
    	margin-bottom: 118px;
    	margin-top: 30px;
    }
    .auto-style2 {
    	margin-left: 0px;
    }
    </style>
    </head>
    <body style="background-color: #C0C0C0">
    <div class="c4" style="height: 120px">
    	<img alt="" height="150" src="masthead.jpg" width="960"></div>
    <form enctype="text/plain" name="addform" method='post' action="acknowledgement.html" id="addform" onSubmit="return submitForm()" class="auto-style2" style="height: 316px; width: 1350px">
    <table border="3" cellpadding="10" class="auto-style1" style="height: 341px; width: 355px;">
    <tr>
    <td align="center" style="height: 149px; width: 1549px"><strong><span class="c1">Join the Kudler Fine Foods Mailing List!<table class="c3" cellspacing="0" cellpadding="2" style="border-style: solid; border-width: 3px; background-color: #FFFFCC; height: 191px; width: 277px;">
    <tr valign="baseline">
    <td><span class="c2">Email Address:</span></td>
    <td>
    <input type="text" name="email" size="38,1" maxlength="80" id="email"></td>
    </tr>
    <tr>
    <td><span class="c2">First Name:</span></td>
    <td><input type="text" name="fname" size="38,1" maxlength="80"></td>
    </tr>
    <tr>
    <td><span class="c2">Last Name:</span></td>
    <td><input type="text" name="lname" size="38,1" maxlength="80"></td>
    </tr>
    <tr>
    <td><span class="c2">Address:</span></td>
    <td><input type="text" name="address" size="38,1" maxlength="80"></td>
    </tr>
    <tr>
    <td><span class="c2">City:</span></td>
    <td><input type="text" name="city" size="38,1" maxlength="80"></td>
    </tr>
    <tr>
    <td><span class="c2">State:</span></td>
    <td><input type="text" name="state" size="10,1" maxlength="25"></td>
    </tr>
    <tr>
    <td><span class="c2">Zip Code:</span></td>
    <td><input type="text" name="zip" size="10,1" maxlength="25" ></td>
    </tr>
    <tr>
    <td style="height: 22px"></td>
    </tr>
    </table>
    </span></strong></td>
    </tr>
    </table>
    <div class="c4" style="width: 310px"><input type="submit" value=" Submit "><input type="reset" value="Reset Form" onclick="resetform()"></div>
    <input type="hidden" name="Form" value="Submit Sub">
    <br>
    </form>
    </body>
    </html>

  • #9
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Which browser are you using? Google Chrome restricts the setting of cookies locally (or it certainly used to..), although this can be unset with a command-line argument.

    Try different browsers. Andy.

    Command-line for Chrome:
    chrome --allow-file-access-from-files
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #10
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AndrewGSW View Post
    Which browser are you using? Google Chrome restricts the setting of cookies locally (or it certainly used to..), although this can be unset with a command-line argument.

    Try different browsers. Andy.

    Command-line for Chrome:
    chrome --allow-file-access-from-files
    Thanks for your input1
    It doesn't matter which browser I use, I just can't seem to get the desired effect which is to set a cookie(emailaddress as the value), get the cookie,read the cookie to compare the value(email address) and then either let the person submit the form OR then propmpt the user thier info has already been submitted and then redirect the user to an error page.

  • #11
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    You should remove the spaces you have between your functions calls:

    Code:
    new Date ();
    return GetCookieVal (j);
    You also have some mis-matched HTML tags and you can't use size="38,1" AFAIK (unless it's allowed with certain character encodings(?)).

    [I haven't looked at your cookie code.]
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #12
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    You should learn to use a console and alert's to debug your code.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #13
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you guys for all your input, you are sure helping better than my flipping teacher.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •