Geeks With Blogs

News
Welcome to my blog.
Here's what we've got on the menu today:

Lorin Thwaits A geek says what?

A question was posed to the Arizona Groups list about how to prevent double-posting to a form on a web page.  My favorite solution to do this is set up a variable to track if the submit has happened yet or not.  The HTML needs to end up looking like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD><title>FormTest</title></HEAD>
 <body>
  <script language="javascript">
   var haveSubmitted=false;
   function FirstSubmitOnly()
   {
    if(haveSubmitted) return false;

haveSubmitted=true; return true;    //we want the first click to occur. } </script> <form name="Form1" id="Form1" method="post" onsubmit="return FirstSubmitOnly();"> <input type="submit" name="ClickMe" value="Click Me!" id="ClickMe" /> </form> </body> </HTML>

In this case the function gets called from the onsubmit event handler on the form element.  But you could also put it on the onclick of the <input type="submit"> as well.  If you're using ASP.NET then this client-side event handler can be wired up in VB.NET with:

Button1.Attributes.Add("onclick","return FirstSubmitOnly();")

Equivalent C# is of course to just add a semicolon at the end of that line.  Hope you find this helpful!

Posted on Sunday, March 12, 2006 5:55 PM ASP.NET | Back to top


Comments on this post: Preventing double-posting on a web page

# re: Preventing double-posting on a web page
Requesting Gravatar...
This shorter option was posted to the list by Michael Ryan:

<form ...
onsubmit="this.onsubmit=function(){return false;}; return true;">

The first time it's triggered it rewrites the function's definition, then returns true. Every time thereafter that rewritten definition for onsubmit() is hit, which returns false.
Left by Lorin on Mar 12, 2006 6:14 PM

# re: Preventing double-posting on a web page
Requesting Gravatar...
This won't work if Javascript is disabled.
Left by J on Mar 13, 2006 6:31 AM

# re: (won't work if Javascript is disabled)
Requesting Gravatar...
True, but quite rare! Those folks can't even use GMail.
Left by Lorin on Mar 13, 2006 11:59 AM

# re: Preventing double-posting on a web page
Requesting Gravatar...
whic is the best??
Left by asd on Dec 03, 2007 10:58 PM

# re: Preventing double-posting on a web page
Requesting Gravatar...
Lorin, thanks for that one!
Left by paardjes on Dec 11, 2008 4:44 AM

# re: Preventing double-posting on a web page
Requesting Gravatar...
how is it possible that a dress made out of duct tape looks like that?!
Left by DDos Protection on Nov 06, 2009 11:33 PM

# re: Preventing double-posting on a web page
Requesting Gravatar...
hey webmaster, I found your website from yahoo answers and read a few of your other blog posts. They are fantastic. Please keep them coming!!! Best regards, Talin.
Left by play puzzle games on Mar 06, 2010 7:09 PM

# re: Preventing double-posting on a web page
Requesting Gravatar...
Every time thereafter that rewritten definition for onsubmit() is hit, which returns false.
Left by gossip girl season on Mar 26, 2010 11:59 AM

# re: Preventing double-posting on a web page
Requesting Gravatar...
Thank you for sharing such good experience.I also like to write such things in own blog. http://www.trustsneakers.com/ nike men's shox turbo shoes go to shopping.
Left by nike men's shox on Jun 11, 2010 2:37 AM

Your comment:
 (will show your gravatar)


Copyright © Lorin Thwaits | Powered by: GeeksWithBlogs.net