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 9 of 9
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Preventing Multiple Form Submits Cookie with a Problem

    Hi,

    since I am not an expert in this field I can't figure out why
    the following cookie does not work, i.e. I can submit the same
    form with the same info over and over and not get to the page
    "AlreadyFilledForm.html" with a message like "you already filled
    out this form".

    I cannot figure out where the problem is or what is wrong in the
    script and make the adjustments needed to debug the script,
    the script follows also a standard form example for testing.
    Please help for any solution...

    Thanks for any help again in advance

    St





    Code:
    <html >
    
    <head>
    <title>Demo</title>
    </head>
    
    <body>
    <script type="text/javascript">
    <!-- 
    
    function validateInformation() {
        if (confirmSubmit() == false)
            return false;
        if (cookieInformationExists() == true) {
            window.location = "AlreadyFilledForm.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!=stelios.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 the form before submitting!");
            return false;
        }
    
        return true;
    }
    
    
     -->
    
    </script>
    <h1>TESTING DEMO</h1>
    <h2>Example of a Mailing List</h2><hr/>
    
    <form name="stelios" action="steliosformprocessor.html" method="post" enctype="text/plain"
        onsubmit="return validateInformation(this)">
    <p>Name<br/>
        <input type="text" name="name" size="30"/></p>
    <p>Address<br/>
        <input type="text" name="address" size="50"/></p>
    <p>City, State, Zip<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<br/>
        <input type="text" name="email" size="50"/></p>
    <p>Phone Number<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"/>
        <input type="reset"/></p>
    
    </form>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,502
    Thanks
    3
    Thanked 500 Times in 487 Posts
    Code:
    <html >
    
    <head>
    <title>Demo</title>
    </head>
    
    <body>
    <script type="text/javascript">
    <!--
    
    function checkForNumber(fieldValue) {
        var numberCheck = isNaN(fieldValue);
        if (numberCheck == true) {
            window.alert("You must enter a numeric value!");
            return false;
        }
    }
    
    
    function confirmSubmit(frm)
    {
     var ary=['name','address','city','zip','areacode','exchange','phone','email'];
     var zxcv='';
     for (var pass=true,zxc0=0;zxc0<ary.length;zxc0++){
      zxcv+=frm[ary[zxc0]].value+':'
      if (frm[ary[zxc0]].value==''){
       pass=false;
      }
     }
     if (!pass){
       alert("You must complete the form before submitting!");
       return false;
     }
     var cookie=zxcReadCookie(frm.name);
     if (cookie&&cookie==zxcv){
      alert('duplicate form');
      return false;
     }
     zxcCreateCookie(frm.name,zxcv,6*30)
     return false; // debug only
     return true;
    }
    
    function zxcCreateCookie(zxcnme,zxcv,zxcdays){
     document.cookie=zxcnme+'='+zxcv+';expires='+(new Date(new Date().getTime()+zxcdays*86400000).toGMTString())+';path=/';
    }
    
    function zxcReadCookie(zxcnme){
     zxcnme+='=';
     var zxcsplit = document.cookie.split(';');
     for(var zxc0=0;zxc0<zxcsplit.length;zxc0++) {
      var zxcs=zxcsplit[zxc0];
      while (zxcs.charAt(0)==' ') zxcs=zxcs.substring(1,zxcs.length);
      if (zxcs.indexOf(zxcnme)==0) return zxcs.substring(zxcnme.length,zxcs.length);
     }
     return null;
    }
    
    
     -->
    
    </script>
    <h1>TESTING DEMO</h1>
    <h2>Example of a Mailing List</h2><hr/>
    
    <form name="stelios" onsubmit="return confirmSubmit(this);" action="steliosformprocessor.html" method="post" enctype="text/plain"
        onsubmit="return validateInformation(this)">
    <p>Name<br/>
        <input type="text" name="name" size="30"/></p>
    <p>Address<br/>
        <input type="text" name="address" size="50"/></p>
    <p>City, State, Zip<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<br/>
        <input type="text" name="email" size="50"/></p>
    <p>Phone Number<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"/>
        <input type="reset"/></p>
    
    </form>
    </body>
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Questions Regarding Preventing Multiple Form Submits Cookie

    Thanks vwphillips,

    for your reply and your excellent dense script!, thank you again!

    I did try it out and no duplicate form can be submitted again for the specified period, everything is working well, except for the following questions:

    -At the end in the html section in the tag:
    Code:
    <form name="stelios" onsubmit="return confirmSubmit(this);" action="steliosformprocessor.html" method="post" enctype="text/plain"
        onsubmit="return validateInformation(this)">
    there are two onsubmits: onsubmit="return confirmSubmit(this);" and onsubmit="return validateInformation(this)"!, since there is no such function like validateInformation() in your script, probably its been left from my script and you have forgotten to delete it?

    -Also, more important, when I press the submit button, it seems that the action="steliosformprocessor.html" of the form tag does not take
    place not calling the steliosformprocessor.html page?? (maybe I'm missing something).

    Maybe you can help me out and explain why this is happening and how to handle it?

    Curiously, when I replace the two onsubmits with each other, so that each one takes the place of the other in the form tag, then the
    duplicate feature does not work, but on the other hand, the page steliosformprocessor.html is being called when I submit the form.

    Thanks for any help in advance

    St

    PS: I tried to solve it but still didn't find out, since I'm not an expert, why in my script the function cookieInformationExists():
    Code:
    function cookieInformationExists() {
        if (document.cookie != "") {
            splitCookie = document.cookie.split("; ");
            userName = unescape(splitCookie[0].split("=")[1]);
            if (userName == "") {
                return false;
            }
            else {
                if (userName!=stelios.name.value)
                    return false;
                return true;
            }
        }
        else {
            return false;
        }
        return false;
    }
    (in conjunction with the rest of the script) does not return true when the same form (userName) is submitted again, maybe somebody can figure out and highlight the problem and why this is happening?

  • #4
    Banned
    Join Date
    May 2005
    Location
    Midwest, U.S.
    Posts
    118
    Thanks
    1
    Thanked 26 Times in 23 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Any Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    
    	var expDate = new Date();
    	expDate.setTime(expDate.getTime()+365*24*60*60*1000); // one year
    
    	function setCookie(isName,isValue,dExpires){
           
    		document.cookie = isName+"="+isValue+";expires="+dExpires.toGMTString();
    	}	
    
    	function getCookie(isName){
    
    		var cookieStr = document.cookie;
    		var startSlice = cookieStr.indexOf(isName+"=");
    		if (startSlice == -1){return false}
    		var endSlice = cookieStr.indexOf(";",startSlice+1)
    		if (endSlice == -1){endSlice = cookieStr.length}
    		var isData = cookieStr.substring(startSlice,endSlice)
    		var isValue = isData.substring(isData.indexOf("=")+1,isData.length);
    		return isValue;
    	}
    
    	function saveForm(){
    
    		var saveString = "";
    		var nText = document.getElementsByTagName('input');
    		for (i=0; i<nText.length-2; i++ )
    			{
    			 saveString += nText[i].value+":";
    			}
    		escape(saveString);
    		setCookie("formData",saveString,expDate);
    	}
    
    	function restoreValues(){
    
    		var nValues = getCookie('formData');
    		var n = 1;
    		if (nValues)
    			{
    			 nValues = nValues.substring(0,nValues.length-1);
    			 unescape(nValues);
    			 var isText = nValues.split(":")			
    			 	for (i=0; i<isText.length; i++)
    					{
    					 document.forms[0][n++].value = isText[i];						
    					}
    			 document.forms[0]['submit'].disabled = true;
    			 document.forms[0]['reset'].disabled = true;
    			 alert('You have already submitted this form');
    			}
    		}
    
    	function validate(nForm){
    
    		var numericDigits = [3,3,4];
    		var nName = nForm['user_name'].value;
    		var nAddress = nForm['address'].value;
    		var nCity = nForm['city'].value; 
    		var nState = nForm['state'].value;
    		var nZip = nForm['zip'].value; 
    		var nAreaCode = nForm['areacode'].value;
    		var nExchange = nForm['exchange'].value;
    		var nPhone = nForm['phone'].value;
    		var nEmail = nForm['email'].value;
    		if (nName == "" || nAddress == "" || nCity == "" || nState == "" || nZip == "")
    			{
    			 alert('Please Complete All Fields');
    			 return false;
    			}
    		if (!/^\d+$/.test(nZip) || nZip.length != 5)
    			{
    			 alert('Incorrect Zip Code Format');
    			 return false; 
    			}
    		if (!/^\w+[\w|\.|\-]{0,1}\w*[\w|\.|\-]{0,1}\w*\@\w+[\w|\-]{0,1}\w*[\w|\-]{0,1}\w*\.{1,1}com|net|edu|mil|org|gov|biz|info$|^\w+[\w|\.|\-]{0,1}\w*[\w|\.|\-]{0,1}\w*\@\w+[\w|\-]{0,1}\w*[\w|\-]{0,1}\w*\.{1,1}[a-z]{2,2}\.{1,1}com|net|edu|mil|org|gov|biz|info$/.test(nEmail))
    			{
    			 alert('Invalid Email Address');
    			 return false; 
    			}
    		var nNumericField = nAreaCode + "|" + nExchange + "|" + nPhone; 
    		nNumericField = nNumericField.split("|");
    		for (i=0; i<nNumericField.length; i++)
    			{
    			 if (!/^\d+$/.test(nNumericField[i]) || nNumericField[i].length != numericDigits[i])
    				{
    				 alert('Incorrect Phone Number Format');
    				 return false;
    				}
    			}		
    		saveForm();
    		alert('Thank you for your submission');
    		return true; 
    	}
    
    	onload = restoreValues;
    	
    </script>
    <style type="text/css">
    
    	 body {background-color: #eae3c6; margin-top: 60px;}
    	 form {width: 620px; margin: auto; font-family: arial; font-size: 10pt;}
    	 fieldset {width: 620px; padding-left: 10px; background-color: #f0fff0; border: 1px solid #87ceeb;}
    	 legend {font-family: tahoma; font-size: 14pt; color: #00008b; background-color: #87ceeb; padding-left: 3px; padding-right: 3px; margin-bottom: 5px;}
    	.submitBtn {font-family: tahoma; font-size: 10pt; display: block; margin-left: auto; margin-right: auto; margin-top: 5px; margin-bottom: 5px;}
    	.resetBtn {font-family: tahoma; font-size: 10pt; display: block; margin-left: auto; margin-right: auto; margin-top: 5px; margin-bottom: 5px;}
    
    </style>
    </head>
    	<body>
    		<form action="" method="post" onsubmit="return validate(this)">
    		   <fieldset>
    			<legend>Form</legend>
    				<label>Name:&nbsp;&nbsp;<input type="text" name="user_name" size="30"></label>	
    				<br><br>			
    				<label>Address:&nbsp;&nbsp;<input type="text" name="address" size="50"></label>
    				<br><br>
    				<label>City:&nbsp;&nbsp;<input type="text" name="city" size="20"></label>
        				<label>State:&nbsp;&nbsp;<input type="text" name="state" size="20"></label>
        				<label>Zip:&nbsp;&nbsp;<input type="text" name="zip" size="7"></label>    
    				<br><br>				
    				<label>E-Mail Address:&nbsp;&nbsp;<input type="text" name="email" size="50" value="my-name.is@sw-fla.rr.com"></label>
    				<br><br>
    				<label>Phone Number:&nbsp;&nbsp;</label>
    				(<input type="text" name="areacode" size="3" maxlength="3">)
        				<input type="text" name="exchange" size="3" maxlength="3">
        				<input type="text" name="phone" size="4" maxlength="4">				
    
    				<input type="submit" name="submit" value="Submit" class="submitBtn">
    				<input type="reset" name="reset" value="Reset" class="resetBtn">
    		   </fieldset>
    		</form>
    	</body>
    </html>
    Last edited by Cranford; 10-08-2008 at 06:52 PM.

  • Users who have thanked Cranford for this post:

    Styl (10-12-2008)

  • #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,502
    Thanks
    3
    Thanked 500 Times in 487 Posts
    Code:
    <html >
    
    <head>
    <title>Demo</title>
    </head>
    
    <body>
    <script type="text/javascript">
    <!--
    
    function checkForNumber(fieldValue) {
        var numberCheck = isNaN(fieldValue);
        if (numberCheck == true) {
            window.alert("You must enter a numeric value!");
            return false;
        }
    }
    
    
    function confirmSubmit(frm)
    {
     var ary=['name','address','city','zip','areacode','exchange','phone','email'];
     var zxcv='';
     for (var pass=true,zxc0=0;zxc0<ary.length;zxc0++){
      zxcv+=frm[ary[zxc0]].value+':'
      if (frm[ary[zxc0]].value==''){
       pass=false;
      }
     }
     if (!pass){
       alert("You must complete the form before submitting!");
       return false;
     }
     var cookie=zxcReadCookie(frm.name);
     if (cookie&&cookie==zxcv){
      alert('duplicate form');
      return false;
     }
     zxcCreateCookie(frm.name,zxcv,6*30)
     return true;
    }
    
    function zxcCreateCookie(zxcnme,zxcv,zxcdays){
     document.cookie=zxcnme+'='+zxcv+';expires='+(new Date(new Date().getTime()+zxcdays*86400000).toGMTString())+';path=/';
    }
    
    function zxcReadCookie(zxcnme){
     zxcnme+='=';
     var zxcsplit = document.cookie.split(';');
     for(var zxc0=0;zxc0<zxcsplit.length;zxc0++) {
      var zxcs=zxcsplit[zxc0];
      while (zxcs.charAt(0)==' ') zxcs=zxcs.substring(1,zxcs.length);
      if (zxcs.indexOf(zxcnme)==0) return zxcs.substring(zxcnme.length,zxcs.length);
     }
     return null;
    }
    
    function validateInformation(frm){
     if (!confirmSubmit(frm)) return false;
     alert('Your Validation Code')
     return false; // debug only
    }
     -->
    
    </script>
    <h1>TESTING DEMO</h1>
    <h2>Example of a Mailing List</h2><hr/>
    
    <form name="stelios" onsubmit="return validateInformation(this);" action="steliosformprocessor.html" method="post" enctype="text/plain"
        >
    <p>Name<br/>
        <input type="text" name="name" size="30"/></p>
    <p>Address<br/>
        <input type="text" name="address" size="50"/></p>
    <p>City, State, Zip<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<br/>
        <input type="text" name="email" size="50"/></p>
    <p>Phone Number<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"/>
        <input type="reset"/></p>
    
    </form>
    </body>
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    Styl (10-10-2008)

  • #6
    New Coder
    Join Date
    Oct 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Adjustments Regarding Preventing Multiple Form Submits Cookie

    Thank you Cranford,

    for your splendid and very elaborated script!, I was not in a position to try it completely out (didn't have the time), so I will post probably tomorrow if anything about it gives any/me trouble, or for any small adjustments made for any arising problems and/or my needs, thank you again Cranford (Midwest, U.S.).

    This also because I was still busy with the excellent script of vwphillips (Portsmouth UK) and how to adjust it and/or solve any problems it was giving me. The changes/adjustments I did on this script, for solving any questions arising from its implementation and/or my needs, are:

    a) since the action in the form tag didn't take place when submitting, I changed and adjusted the tag to:

    Code:
    <form name="stelios" onsubmit="confirmSubmit(this);" action="steliosformprocessor.html" method="post" enctype="text/plain"
       >
    so that the action is also triggered when submitting the form for testing ect.

    b) there was also a problem, since the form was submitted also empty or void, after the alert (you must complete the form before submitting!); to avoid this I inserted in the function confirmSubmit(frm) after the corresponding alert:

    ...alert("You must complete the form before submitting!");
    Code:
       document.write("<html><head><title>"
           +"The form cannot be submitted void!</title>"
           +"</head><body><center>"
           +"<h1>You have to fill out the form before submitting!</h1><br><h3><i>"
           +"You cannot submit an empty form!"
           +"</h3><br><h4> Thank you for your interest.</i></h4>"
           +"</center></body></html>");
    now the form does not submit by itself void, but the above message instead is written "You have to fill out the form before submitting etc."

    c) there was also the same problem when the form was submitted again, after the alert "The form as already been submitted" the action in the form tag was triggered anyway, so I inserted also in the above function after the corresponding alert:

    ...alert("You have already submitted this form");
    Code:
       document.write("<html><head><title>"
           +"The form has already been submitted!</title>"
           +"</head><body><center>"
           +"<h1>You have already submitted this form!</h1><br><h3><i>"
           +"You cannot submit the form again, until two months "
           +"(since the day you first submitted it) have elapsed."
           +"</h3><br><h4> Thank you for your interest.</i></h4>"
           +"</center></body></html>");
    now instead of submitting itself duplicate the form it shows the message "You have already submitted this form ect."

    d) I inserted also in the function zxcCreateCookie(...) for obvious reasons, the alert:

    Code:
    alert("Submitting Form....\nThank you!");
    now the user will be instantly informed when his form is being submitted.

    I hope that the changes/adjustemts I made are in the right direction, in any case they suit my needs better. Probably tomorrow
    I will post also about the script of Cranford, since I did there also some small adjustments to solve the problems I did face. Just before posting I did read also the new posting of vwphillips with his extended excellent script, so I couldn't try the new functions in it out, for anything about it I will post separately.

    Thanks for all the help so much (from all) in advance

    St


    PS: Now the whole script looks like this (I did also some adjustments in the form in a way so that it can suit and reflect better also the conditions in Continental Europe):

    Code:
    <html >
    
    <head>
    <title>Demo</title>
    </head>
    
    <body>
    <script type="text/javascript">
    <!--
    
    function checkForNumber(fieldValue) {
        var numberCheck = isNaN(fieldValue);
        if (numberCheck == true) {
            window.alert("You must enter a numeric value!");
            return false;
        }
    }
    
    
    function confirmSubmit(frm)
    {
     var ary=['name','address','city','zip','areacode','phone','email'];
     var zxcv='';
     for (var pass=true,zxc0=0;zxc0<ary.length;zxc0++){
      zxcv+=frm[ary[zxc0]].value+':'
      if (frm[ary[zxc0]].value==''){
       pass=false;
      }
     }
     if (!pass){
       alert("You must complete the form before submitting!");
        document.write("<html><head><title>"
           +"The form cannot be submitted void!</title>"
           +"</head><body><center>"
           +"<h1>You have to fill out the form before submitting!</h1><br><h3><i>"
           +"You cannot submit an empty form!"
           +"</h3><br><h4> Thank you for your interest.</i></h4>"
           +"</center></body></html>");
    
       return false;
     }
     var cookie=zxcReadCookie(frm.name);
     if (cookie&&cookie==zxcv){
        alert("You have already submitted this form");
        document.write("<html><head><title>"
           +"The form has already been submitted!</title>"
           +"</head><body><center>"
           +"<h1>You have already submitted this form!</h1><br><h3><i>"
           +"You cannot submit the form again, until two months "
           +"(since the day you first submitted it) have elapsed."
           +"</h3><br><h4> Thank you for your interest.</i></h4>"
           +"</center></body></html>");
    
      return false;
     }
     zxcCreateCookie(frm.name,zxcv,2*30)
     return false; // debug only
     return true;
    }
    
    function zxcCreateCookie(zxcnme,zxcv,zxcdays){
     document.cookie=zxcnme+'='+zxcv+';expires='+(new Date(new Date().getTime()+zxcdays*24*60*60*1000).toGMTString())+';path=/';
    alert("Submitting Form....\nThank you!"); 
      return true;
    
    }
    
    function zxcReadCookie(zxcnme){
     zxcnme+='=';
     var zxcsplit = document.cookie.split(';');
     for(var zxc0=0;zxc0<zxcsplit.length;zxc0++) {
      var zxcs=zxcsplit[zxc0];
      while (zxcs.charAt(0)==' ') zxcs=zxcs.substring(1,zxcs.length);
      if (zxcs.indexOf(zxcnme)==0) return zxcs.substring(zxcnme.length,zxcs.length);
     }
     return null;
    }
    
    
     -->
    
    </script>
    <h1>TESTING DEMO</h1>
    <h2>Example of a Mailing List</h2><hr/>
    
    <form name="stelios" onsubmit="confirmSubmit(this);" action="steliosformprocessor.html" method="post" enctype="text/plain"
       > <pre>
    <p>Name:           <input type="text" name="name" size="30"/></p>
    <p>Address:        <input type="text" name="address" size="50"/></p>
    <p>City,State,Zip: <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: <input type="text" name="email" size="50"/></p>
    <p>Phone Number:  (<input type="text" name="areacode" size="3" maxlength="3"
        onchange="return checkForNumber(this.value);"/>)<input type="text" name="phone" size="12" maxlength="12"
        onchange="return checkForNumber(this.value);"/></p>
    <p>Submit object   <input type="submit" value="Submit"/>
    Reset object    <input type="reset"/></p>
    </pre>
    </form>
    </body>
    </html>

  • #7
    New Coder
    Join Date
    Oct 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation Questions Regarding Preventing Multiple Form Submits Cookie (Part II)

    Thanks vwphillips,

    for your extended script (posted 10-08-2008), on the other hand I am still facing the same problems also with the previous script, i.e.:

    1) when I press the button submit, after having filled out correctly the form, the action in the form tag is not triggered ( i.e. the page set as example steliosformprocessor.html is not being called), instead always the script gives the alert "Your Validation Code"! Why does in the script the function:
    Code:
    function validateInformation(frm){
     if (!confirmSubmit(frm)) return false;
     alert('Your Validation Code')
     return false; // debug only
    }
    always give an alert, even if the form is being filled correctly out when submitting, without triggering the action in the form tag, how to fix this? I haven't figured out where the problem lies ( in this function and/or in the function confirmSubmit(frm) and/or) in the script itself and what adjustments to make? Maybe someone knows the answer and how to fix the problem and make the script work?

    ( In the script of Cranford when you press the submit button, created from the tag
    Code:
     <form action="steliosformprocessor.html" method="post" onsubmit="return validate(this)">
    ,after having filled out correctly the form, the action in the form tag is automatically triggered so that the form is really submitted (I have inserted there also the example page action="steliosformprocessor.html" which is therefor always called when the form is filled out submitted!).

    2) When I change (as already mentioned in my previous posting) the onsubmit in the form tag from onsubmit="return validateInformation(this);" to onsubmit="validateInformation(this);", the submit action is being triggered when I submit!

    On the other hand we want to stop the submit of the form if some of the fields are not filled in! So, as events are stopped if the event handler code returns ""false", we have to change the onsubmit to: onsubmit="return validateInformation (this);" to let that happen!

    Mooving in the different direction, with the onsubmit set simply to "validateInformation (this);", I have made some changes, as presented in my former posting (10-08-2008), so that also in this case the script works when the form is being submitted empty, giving the message "You have to fill out the form before submitting etc.". But with such a manipulation of the script, other problems arise, like that the user has
    to load back the previous page to get back to the form if he wants to fill it out etc. etc.

    Maybe can you vwphillips (or somebody else) help us out and please highlight where the problem lies in the above script, and how to fix it so that the script can be used as it is presented without rough manipulations!

    Thanks for any help again


    St

  • #8
    New Coder
    Join Date
    Oct 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Arrow Adjustments Made In Preventing Multiple Form Submits Cookie

    I just want to thank Cranford for his script, it works in every direction, also disabling the submit and reset button until the input is considered valid...

    I just needed to made 3 adjustments so that it can be easily used through Continental Europe and for my needs... etc:

    a) In the original script the Zip Code could be only accepted exactly as a 5 digit number, I have changed that so that somebody can insert up to 7 digits in the Zip Code without any problems.

    b) Also in the original script the Phone Number could only accept no more or no less but exactly 3 digits area code and the same (only 3 digits) for the exchange code and only 4 digits for the phone number. I did change that in a way that you can insert up to 6 digits area code and a phone number up to 12 digits, for obvious reasons so that someone can submit a form also from e.g. Greece or Spain in Europe etc.

    Thanks again
    I just don't think that anyone will ever highlight or find out why my (first above in the thread) script does not work properly, but if so, please post here your answer, tomorrow or whenever but please do post...

    St

    PS: The code for the script, which can accept now without problems submissions from Europe and the rest of the world, after the 3 above adjustments is:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Any Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    
    	var expDate = new Date();
    	expDate.setTime(expDate.getTime()+365*24*60*60*1000); // cookie expires after 365 days, change to whatever
    
    	function setCookie(isName,isValue,dExpires){
           
    		document.cookie = isName+"="+isValue+";expires="+dExpires.toGMTString();
    	}	
    
    	function getCookie(isName){
    
    		var cookieStr = document.cookie;
    		var startSlice = cookieStr.indexOf(isName+"=");
    		if (startSlice == -1){return false}
    		var endSlice = cookieStr.indexOf(";",startSlice+1)
    		if (endSlice == -1){endSlice = cookieStr.length}
    		var isData = cookieStr.substring(startSlice,endSlice)
    		var isValue = isData.substring(isData.indexOf("=")+1,isData.length);
    		return isValue;
    	}
    
    	function saveForm(){
    
    		var saveString = "";
    		var nText = document.getElementsByTagName('input');
    		for (i=0; i<nText.length-2; i++ )
    			{
    			 saveString += nText[i].value+":";
    			}
    		escape(saveString);
    		setCookie("formData",saveString,expDate);
    	}
    
    	function restoreValues(){
    
    		var nValues = getCookie('formData');
    		var n = 1;
    		if (nValues)
    			{
    			 nValues = nValues.substring(0,nValues.length-1);
    			 unescape(nValues);
    			 var isText = nValues.split(":")			
    			 	for (i=0; i<isText.length; i++)
    					{
    					 document.forms[0][n++].value = isText[i];						
    					}
    			 document.forms[0]['submit'].disabled = true;
    			 document.forms[0]['reset'].disabled = true;
    			 alert('You have already submitted this form');
    			}
    		}
    
    	function validate(nForm){
    
    		var numericDigits = [3,3,4];
    		var nName = nForm['user_name'].value;
    		var nAddress = nForm['address'].value;
    		var nCity = nForm['city'].value; 
    		var nState = nForm['state'].value;
    		var nZip = nForm['zip'].value; 
    		var nAreaCode = nForm['areacode'].value;
    	              var nPhone = nForm['phone'].value;
    		var nEmail = nForm['email'].value;
    		if (nName == "" || nAddress == "" || nCity == "" || nState == "" || nZip == "")
    			{
    			 alert('Please Complete All Fields');
    			 return false;
    			}
    		if (!/^\d+$/.test(nZip))
    			{
    			 alert('Incorrect Zip Code Format');
    			 return false; 
    			}
    		if (!/^\w+[\w|\.|\-]{0,1}\w*[\w|\.|\-]{0,1}\w*\@\w+[\w|\-]{0,1}\w*[\w|\-]{0,1}\w*\.{1,1}com|net|edu|mil|org|gov|biz|info$|^\w+[\w|\.|\-]{0,1}\w*[\w|\.|\-]{0,1}\w*\@\w+[\w|\-]{0,1}\w*[\w|\-]{0,1}\w*\.{1,1}[a-z]{2,2}\.{1,1}com|net|edu|mil|org|gov|biz|info$/.test(nEmail))
    			{
    			 alert('Invalid Email Address');
    			 return false; 
    			}
    		var nNumericField = nAreaCode + "|" + nPhone; 
    		nNumericField = nNumericField.split("|");
    		for (i=0; i<nNumericField.length; i++)
    			{
    			 if (!/^\d+$/.test(nNumericField[i]))
    				{
    				 alert('Incorrect Phone Number Format');
    				 return false;
    				}
    			}		
    		saveForm();
    
    		alert('Thank you for your submission');
    		return true; 
    	}
    
    	onload = restoreValues;
    	
    </script>
    <style type="text/css">
    
    	 body {background-color: #eae3c6; margin-top: 60px;}
    	 form {width: 620px; margin: auto; font-family: arial; font-size: 10pt;}
    	 fieldset {width: 620px; padding-left: 10px; background-color: #f0fff0; border: 1px solid #00ffff;}
    	 legend {font-family: tahoma; font-size: 14pt; color: #00008b; background-color: cyan; padding-left: 3px; padding-right: 3px; margin-bottom: 5px;}
    	.submitBtn {font-family: tahoma; font-size: 10pt; display: block; margin-left: auto; margin-right: auto; margin-top: 5px; margin-bottom: 5px;}
    	.resetBtn {font-family: tahoma; font-size: 10pt; display: block; margin-left: auto; margin-right: auto; margin-top: 5px; margin-bottom: 5px;}
    
    </style>
    </head>
    	<body>
    		<form action="steliosformprocessing.html" method="post" onsubmit="return validate(this)">
    		   <fieldset>
    			<legend>Form</legend>
    				<label>Name:&nbsp;&nbsp;<input type="text" name="user_name" size="30"></label>	
    				<br><br>			
    				<label>Address:&nbsp;&nbsp;<input type="text" name="address" size="50"></label>
    				<br><br>
    				<label>City:&nbsp;&nbsp;<input type="text" name="city" size="20"></label>
        				<label>State:&nbsp;&nbsp;<input type="text" name="state" size="20"></label>
        				<label>Zip:&nbsp;&nbsp;<input type="text" name="zip" size="7"></label>    
    				<br><br>				
    				<label>E-Mail Address:&nbsp;&nbsp;<input type="text" name="email" size="50" value=""></label>
    				<br><br>
    				<label>Phone Number:&nbsp;&nbsp;</label>
    				(<input type="text" name="areacode" size="6" maxlength="6">)
        				
        				<input type="text" name="phone" size="12" maxlength="12">				
    
    				<input type="submit" name="submit" value="Submit" class="submitBtn">
    				<input type="reset" name="reset" value="Reset" class="resetBtn">
    		   </fieldset>
    		</form>
    	</body>
    </html>

  • #9
    New Coder
    Join Date
    Oct 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi,

    I'm just checking what did happen to my post here.

    Regards, St


  •  

    Posting Permissions

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