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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Location
    California
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reading Data from Cookie

    I have created a basic form in JavaScript that asks for user data, I then store that data in a cookie. What I need is if the user comes back to the page and fills in the same data I want to re-direct the user to another page stating that has already been entered. Right now I know I am able to read the cookie because I put in a test message on page load to show me the data. I just don't know how to compare the data in the cookie with the data entered and then re-direct if the data matches....

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtmll/DTD/xhtmll-strict.dtd">
    <html>
    <head>
    <title>Kudler Fine Foods Customer Form</title>
    <style type="text/css">
    	body { font-family: "Trebuchet MS", Arial, Helvetica, sans-serif, serif}
    	h2 { font-size: 1.2em }
    </style>
    <script type=text/javascript>
    <!--HIDE FROM INCOMPATIBLE BROWSERS
    function checkForNumber(fieldValue) {
    		var numberCheck = isNaN(fieldValue);
    		if (numberCheck == true) {
    				window.alert("You must enter a numeric value!");
    				return false;
    		}
    	}
    function confirmPassword() {
    		if (document.forms[0].password.value !=
    				document.forms[0].password_confirm.value) {
    					window.alert("You did not enter the same password!");
    						document.forms[0].password.focus();
    					}
    		}
    function ensurePasswordConfirm () {
    		if (!document.forms[0].password_confirm.focus()) {
    				window.alert("You must confirm the password you entered!");
    			document.forms[0].password_confirm.focus();
    			}
    }
    function confirmSubmit() {
    		if (document.forms[0].name.value == "") {
    			window.alert("You must enter your name!!");
    			return false;
    		}
    		if (document.forms[0].address.value == "" 
    			|| document.forms[0].email_address.value == "")	{
    			window.alert("You must enter your address or email address!!");
    			return false;
    		}
    }
    function confirmReset () {
    		var resetForm = window.confirm("Are you sure you want to reset the form?");
    			if (resetForm == true)
    					return true;
    			return false;
    }
    function setCookie () {
    		var expiresDate = new Date();
    		expiresDate.setFullYear(expiresDate.getFullYear() + 1);
    		document.cookie = encodeURI ("name=" 
    			+ document.forms[0].name.value) + "; expires="
    			+ expiresDate.toUTCString ();
    		document.cookie = encodeURI ("address="
    			+ document.forms[0].address.value) + "; expires="
    			+ expiresDate.toUTCString ();
    	}
    function getCookie () {
    		var getData = decodeURI(document.cookie);
    		var data = getData.split("; ")
    		var name = data[0].split("=");
    		var address = data[1].split("=");
    		window.alert("Welcome " + name[1] + " Your address is " + address[1]);
    	}
    		
    //STOP HIDING FROM INCOMPATIBLE BROWSERS -->
    </script>
    </head>
    <body onload="getCookie();" id="docBody">
    <h1>Kudler Fine Foods Customer Information Page</h2>
    <form action="FormProcessor.html" method="get"
    		enctype="application/x-www-form-urlencoded"
    		onsubmit="return confirmSubmit();"
    		onreset="return confirmReset();">
    <table border="0" cellpadding="3" cellspacing="0">
    		<tr>
    			<td valign="top">
    				<p>Name<br />
    					<input type="text" name="name" size="50" />
    				</p>
    			
    				<p>Address<br />
    					<input type="text" name="address" size="50" onchange="setCookie();" />
    				</p>				
    				<p>City, State, Zip<br />
    					<input type="text" name="city" size="34" />
    					<input type="text" name="state" size="2" maxlength="2" />
    					<input type="text" name="zip" size="5" maxlength="5" onchange="return checkForNumber(this.value);" />
    				</p>
    				<p>Telephone<br />
    					(<input type="text" name="area" size="3" maxlength="3" />) <input
    							type="text" name="exchange" size="3" maxlength="3" />
    					<input type="text" name="phone" size="4" maxlength="4" onchange="return checkForNumber(this.value);" /></p>
    				<p>E-Mail Address</br />
    					<input type="text" name="email_address" size="70" />
    				</p>
    				<p>Enter a password that you can use to<br /> 
    						manage your subscription online:<br />
    					<input type="password" name="password" size="50" 
    						onchange="ensurePasswordConfirm();"/></p>
    				<p>Type the password again to confrim it.<br />
    					<input type="password" name="password_confirm" size="50"
    						onchange="confirmPassword();" />
    				</p>
    				<p>
    					<input type="submit" value="Submit Form" />
    					<input type="reset" />
    				</p>
    				
    
    			</td>
    		</tr>
    		
    </form>				
    </body>
    </html>

  • #2
    New to the CF scene
    Join Date
    Apr 2008
    Location
    California
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    getting closer....

    I think I am getting closer, I know I able to read the data in the cookie because I have a window.alert that displays the data. What I need to do know is compare the data in the cookie with data that is entered on return to the form and if it matches direct the user to a different url. I added a function that should load when the user clicks the submit button. At this point the form is allowing for duplicate data to be entered. Meaning you can put the same exact information in all of the fields and it doesn't "error" out. At this point I am only checking the name and address field.


    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtmll/DTD/xhtmll-strict.dtd">
    <html>
    <head>
    <title>Kudler Fine Foods Customer Form</title>
    <style type="text/css">
    	body { font-family: "Trebuchet MS", Arial, Helvetica, sans-serif, serif}
    	h2 { font-size: 1.2em }
    </style>
    <script type=text/javascript>
    <!--HIDE FROM INCOMPATIBLE BROWSERS
    function checkForNumber(fieldValue) {
    		var numberCheck = isNaN(fieldValue);
    		if (numberCheck == true) {
    				window.alert("You must enter a numeric value!");
    				return false;
    		}
    	}
    function confirmPassword() {
    		if (document.forms[0].password.value !=
    				document.forms[0].password_confirm.value) {
    					window.alert("You did not enter the same password!");
    						document.forms[0].password.focus();
    					}
    		}
    function ensurePasswordConfirm () {
    		if (!document.forms[0].password_confirm.focus()) {
    				window.alert("You must confirm the password you entered!");
    			document.forms[0].password_confirm.focus();
    			}
    }
    function confirmSubmit() {
    		if (document.forms[0].name.value == "") {
    			window.alert("You must enter your name!!");
    			return false;
    		}
    		if (document.forms[0].address.value == "" 
    			|| document.forms[0].email_address.value == "")	{
    			window.alert("You must enter your address or email address!!");
    			return false;
    		}
    }
    function confirmReset () {
    		var resetForm = window.confirm("Are you sure you want to reset the form?");
    			if (resetForm == true)
    					return true;
    			return false;
    }
    function setCookie () {	
    		var expiresDate = new Date();
    		expiresDate.setFullYear(expiresDate.getFullYear() + 1);
    		document.cookie = encodeURI ("name=" 
    			+ document.forms[0].name.value) + "; expires="
    			+ expiresDate.toUTCString ();
    		document.cookie = encodeURI ("address="
    			+ document.forms[0].address.value) + "; expires="
    			+ expiresDate.toUTCString ();
    		}
    function getCookie () {
    	if(document.cookie.length > 0) {
    		var getData = decodeURI(document.cookie);
    		var data = getData.split("; ")
    		var name = data[0].split("=");
    		var address = data[1].split("=");
    		window.alert("Welcome " + name[1] + " Your address is " + address[1]);		
    	}	
    }
    function checkData () {
     if ((name[1] == document.forms[0].name.value) && (address[1] == document.forms[0].address.value)){
      window.alert("You already filled this out!!!!");
      }
     }
    //STOP HIDING FROM INCOMPATIBLE BROWSERS -->
    </script>
    </head>
    <body onload="getCookie();" id="docBody">
    <h1>Kudler Fine Foods Customer Information Page</h2>
    <form action="FormProcessor.html" method="get"
    		enctype="application/x-www-form-urlencoded"
    		onsubmit="return confirmSubmit();"
    		onreset="return confirmReset();">
    <table border="0" cellpadding="3" cellspacing="0">
    		<tr>
    			<td valign="top">
    				<p>First & Last Name<br />
    					<input type="text" name="name" size="50" />
    				</p>
    						
    				<p>Address<br />
    					<input type="text" name="address" size="50" onchange="checkData();"/>
    				</p>				
    				<p>City, State, Zip<br />
    					<input type="text" name="city" size="34" />
    					<input type="text" name="state" size="2" maxlength="2" />
    					<input type="text" name="zip" size="5" maxlength="5" onchange="return checkForNumber(this.value);" />
    				</p>
    				<p>Telephone<br />
    					(<input type="text" name="area" size="3" maxlength="3" />) <input
    							type="text" name="exchange" size="3" maxlength="3" />
    					<input type="text" name="phone" size="4" maxlength="4" onchange="return checkForNumber(this.value);" /></p>
    				<p>E-Mail Address</br />
    					<input type="text" name="email_address" size="70" onchange="setCookie();"/>
    				</p>
    				<p>Enter a password that you can use to<br /> 
    						manage your subscription online:<br />
    					<input type="password" name="password" size="50" 
    						onchange="ensurePasswordConfirm();"/></p>
    				<p>Type the password again to confrim it.<br />
    					<input type="password" name="password_confirm" size="50"
    						onchange="confirmPassword();" />
    				</p>
    				<p>
    					<input type="submit" value="Submit Form" onclick="checkData();" />
    					<input type="reset" />
    				</p>
    				
    
    			</td>
    		</tr>
    		
    </form>				
    </body>
    </html>


  •  

    Posting Permissions

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