• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

can anyone tweak my form cookie?


I got this script from Netmag the other day and I'd like to tweak it a bit but I can't seem to get it right.

The script warns users that they have already submitted a form when they click on submit, it gives the date/time of submission and gives then the option to re-submit.

And it goes like this:

-------------------------

<FORM ACTION="http://www.netmag.co.uk" NAME="myform">
<INPUT TYPE=BUTTON VALUE="Submit This Form" onClick="mySubmit()">
</FORM>

</CENTER>

<SCRIPT>
var alreadyclicked = "no";
var cookiename = "datesubmitted";

function mySubmit(){
   if (alreadyclicked == "yes"){
      alert("Form already being processed.");
      return;
   }

   if (document.cookie.indexOf(cookiename) > -1){
      var mystart =document.cookie.indexOf(cookiename);
      var myend = document.cookie.indexOf(";", mystart);
      if (myend < 0){myend = document.cookie.length;}
      var mydate = document.cookie.substring(mystart +
cookiename.length + 1, myend+1);

      var myconfirm = "You submitted this form at ";
      myconfirm = myconfirm + mydate;
      myconfirm = myconfirm + ". Submit again?"
      ret = confirm(myconfirm);
      if (ret == false){
         return;
      }
   }

   mydate = new Date();
   document.cookie = cookiename + "=" + mydate;
   alreadyclicked = "yes";
   document.myform.submit();
}

</SCRIPT>

------------------

I would like to tweak this script in two ways.

Firstly, I would like to REMOVE the option to re-submit,
and
secondly, I would like to set an expiry date of 3 months.

Any hot tips would be massively gratefully received.

Thanks.
0
GabrielleReid
Asked:
GabrielleReid
  • 2
1 Solution
 
TTomCommented:
Without getting too messy, it seems that setting the expiry date is a matter of changing the date on your cookie setting.

In order to prevent the resubmission, all you need to do is to determine if the cookie exists.  If it does, the form should not be resubmitted.


function mySubmit() {
cookiename = "alreadysubmitted"
if (document.cookie.indexOf(cookiename) > -1){
  alert("You can only submit this form once every three months."
  return false;
} else {
  mydate = new Date();
  mydate.setMonth(mydate.getMonth() + 3)
  document.cookie = cookiename + "=" + mydate;
  return true;
}

<form onsubmit="return mySubmit();">

This should do it for you.

Tom
0
 
COBOLdinosaurCommented:
This question has been abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.
<note>
   In the absence of responses, I may recommend DELETE unless it is clear
   to me that it has value as a PAQ.  Silence = you don't care
</note>

Cd&
0
 
COBOLdinosaurCommented:
It is time to clean this abandoned question up.  

I am putting it on a clean up list for CS.

<recommendation>
points to TTOM

</recommendation>

If anyone participating in the Q disagrees with the recommendation,
please leave a comment for the mods.

Cd&
0
 
Computer101Commented:
Comment from expert accepted a answer

Computer101
E-E Admin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now