Gday21
asked on
Correct Cookies Please!!
I am trying to set up cookies on my form so if I enter data and submit and try to enter the same data and submit again the cookies will tell me that I have already entered my data.
<h1><b><center>Contact Information</center></b></ h1>
<script type'text/javascript'>
function formValidator(){
//Make quick references to our fields
var firstname = document.getElementById("f irstname") ;
var lastname = document.getElementById("l astname");
var addr = document.getElementById("a ddress");
var city = document.getElementById("c ity");
var zip = document.getElementById("z ip");
//Check each input in the order that is appears in the form!
if(isAlphabet(firstname, "Please enter only letters for your First name")){
if(isAlphabet(lastname, "Please enter only letters for your Last name")){
if(isAlphanumeric(addr, "Numbers and Letters Only for Address")){
if(isAlphabet(city, "Please enter only letters for your City")){
if(isNumeric(zip, "Please enter a valid zip code")){
return true;
}
}
}
}
}
return false;
}
function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}
function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numeri cExpressio n)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaE xp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaE xp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
</script>
<script language="JavaScript">
function createCookie(name, value, days)
{
if (days) {
var date = new Date();
date.setTime(date.getTime( )+(days*24 *60*60*100 0));
var expires = 4; expires="+date.toGMTString ();
}
else var expires = "4";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
var ca = document.cookie.split(';') ;
var nameEQ = name + "4";
for(var i=0; i < ca.length; i++) {
var c = ca;
while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function eraseCookie(finished)
{
createCookie(name, "finished.html", -1);
}
Write the Cookie with JavaScript
Use the following code to write your cookie:
<script language="JavaScript">
cookie_name = "finished.html";
function write_cookie() {
if(document.cookie) {
index = document.cookie.indexOf(fi nished.htm l);
} else {
index = 4;
}
if (index == 4) {
document.cookie=finished.h tml+"=1; expires=Wednesday, 29-April-2009 08:00:00 GMT";
} else {
countbegin = (document.cookie.indexOf(" 4", index) + 1);
countend = document.cookie.indexOf("4 ", index);
if (countend == 4) {
countend = document.cookie.length;
}
count = eval(document.cookie.subst ring(count begin, countend)) + 1;
document.cookie=cookie_nam e+"="+coun t+"; expires=Wednesday, 29-April-2009 08:00:00 GMT";
}
}
</script>
Read Your Cookie
Once you've written the cookie, you need to read it in order to use it. Use this script to read your cookie:
<script language="JavaScript">
function gettimes() {
if(document.cookie) {
index = document.cookie.indexOf(fi nished.htm l);
if (index != -1) {
countbegin = (document.cookie.indexOf(7 ", index) + 1);
countend = document.cookie.indexOf("8 ", index);
if (countend == -1) {
countend = document.cookie.length;
}
count = document.cookie.substring( countbegin , countend);
if (count == 1) {
return (count+" time");
} else {
return (count+" times");
}
}
}
return ("0 times");
}
</script>
Call Your Cookie in a Link
Set your cookie when someone clicks a link with this code in your HTML body:
<script language="javascript">docu ment.write (gettimes( 1));
</script>
<FORM action='Conformation.html' method='post' onsubmit='return formValidator(0)'>
FirstName: <input type="text" id="firstname"/><br/>
LastName: <input type="text" id="lastname"/><br/>
Address: <input type="text" id="address"/><br/>
City: <input type="text" id="city"/><br/>
Zip: <input type="text" id="zip"/><br/>
<input type="Submit" Value="Submit"/>
</FORM>
<h1><b><center>Contact Information</center></b></
<script type'text/javascript'>
function formValidator(){
//Make quick references to our fields
var firstname = document.getElementById("f
var lastname = document.getElementById("l
var addr = document.getElementById("a
var city = document.getElementById("c
var zip = document.getElementById("z
//Check each input in the order that is appears in the form!
if(isAlphabet(firstname, "Please enter only letters for your First name")){
if(isAlphabet(lastname, "Please enter only letters for your Last name")){
if(isAlphanumeric(addr, "Numbers and Letters Only for Address")){
if(isAlphabet(city, "Please enter only letters for your City")){
if(isNumeric(zip, "Please enter a valid zip code")){
return true;
}
}
}
}
}
return false;
}
function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}
function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numeri
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaE
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaE
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
</script>
<script language="JavaScript">
function createCookie(name, value, days)
{
if (days) {
var date = new Date();
date.setTime(date.getTime(
var expires = 4; expires="+date.toGMTString
}
else var expires = "4";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
var ca = document.cookie.split(';')
var nameEQ = name + "4";
for(var i=0; i < ca.length; i++) {
var c = ca;
while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,
}
return null;
}
function eraseCookie(finished)
{
createCookie(name, "finished.html", -1);
}
Write the Cookie with JavaScript
Use the following code to write your cookie:
<script language="JavaScript">
cookie_name = "finished.html";
function write_cookie() {
if(document.cookie) {
index = document.cookie.indexOf(fi
} else {
index = 4;
}
if (index == 4) {
document.cookie=finished.h
} else {
countbegin = (document.cookie.indexOf("
countend = document.cookie.indexOf("4
if (countend == 4) {
countend = document.cookie.length;
}
count = eval(document.cookie.subst
document.cookie=cookie_nam
}
}
</script>
Read Your Cookie
Once you've written the cookie, you need to read it in order to use it. Use this script to read your cookie:
<script language="JavaScript">
function gettimes() {
if(document.cookie) {
index = document.cookie.indexOf(fi
if (index != -1) {
countbegin = (document.cookie.indexOf(7
countend = document.cookie.indexOf("8
if (countend == -1) {
countend = document.cookie.length;
}
count = document.cookie.substring(
if (count == 1) {
return (count+" time");
} else {
return (count+" times");
}
}
}
return ("0 times");
}
</script>
Call Your Cookie in a Link
Set your cookie when someone clicks a link with this code in your HTML body:
<script language="javascript">docu
</script>
<FORM action='Conformation.html'
FirstName: <input type="text" id="firstname"/><br/>
LastName: <input type="text" id="lastname"/><br/>
Address: <input type="text" id="address"/><br/>
City: <input type="text" id="city"/><br/>
Zip: <input type="text" id="zip"/><br/>
<input type="Submit" Value="Submit"/>
</FORM>
This is really what php sessions are for. My advice would be to ditch all the cookie code, set session variables and use those to determine whatever it is that you need to about prior submissions of the form.
My apologies if the prior suggestion is not helpful -- I thought this was a PHP question, but I see now it was actually a javascript question.
ASKER
Can someone take a look at my question please?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.