Solved

can anyone tweak my form cookie?

Posted on 2001-06-02
4
153 Views
Last Modified: 2011-09-20

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
Comment
Question by:GabrielleReid
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
TTom earned 50 total points
ID: 6149193
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 7811048
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 7909538
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
 
LVL 1

Expert Comment

by:Computer101
ID: 7958421
Comment from expert accepted a answer

Computer101
E-E Admin
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now