Google Answers Logo
View Question
Q: JavaScript: Form: Submit only once ( Answered 5 out of 5 stars,   0 Comments )
Subject: JavaScript: Form: Submit only once
Category: Computers > Internet
Asked by: cclegg06-ga
List Price: $10.00
Posted: 30 Mar 2003 10:38 PST
Expires: 29 Apr 2003 11:38 PDT
Question ID: 183250
I would like prevent someone from submitting a form more than once
(and hence avoiding double submissions in the data file).

The script should be called from an external file.  Please provide
both a simple JavaScript external file script and html code.
Subject: Re: JavaScript: Form: Submit only once
Answered By: theta-ga on 30 Mar 2003 12:54 PST
Rated:5 out of 5 stars
Hi cclegg06-ga,
   I have used the technique outlined below to detect and prevent
multiple form submissions.
   Whenever the user clicks the submit button, the javascript function
submitForm() is called. This function returns true if form submission
is allowed and false if it is to be prevented. It tracks whether the
submit button has been pressed before or not, with the help of a
global variable named formSubmitted.
   This variable initially has the value false. Whenever the user
submits the form, this variable is set to true. If the user clicks
submit again, the function detects that the user has already submitted
the form, since the formSubmitted variable is set to true, and so
returns false, preventing the form from being submitted.

   Since the user is only allowed to press the submit button once, I
have included an extra layer of verification in the code. The
formSubmit() function first checks to see whether all the required
data in the form has been filled or not by calling the verifyForm()
function. If the form has not been completely filled, a message is
shown to the user, and the click is not counted. If all the data has
been filled, then the verifyForm() function returns true, and the
formSubmit() function continues. Please note that in my sample code, I
have left this function empty. It always returns true. You should
modify the code to check your form for completeness. Or, you can
remove it entirely, if you so want.

You can get the javascript file and the example HTML page from the
following links:
 [ External JS File ]
 [ Example HTML Page]

For your convenience, I also include the source code below:
====================BEGIN HTML============================
  <script  type='text/javascript' language="javascript"
   <INPUT TYPE="text" NAME="text_field">

   <input type="Submit" value="Submit" onClick="return submitForm();">

===================== END HTML============================

==================== BEGIN Submit.js ============================
// Initialize our submit flag to false
var formSubmitted=false;

function submitForm()
  //has the form been submitted before?
  if( formSubmitted == true )
     alert("This form has already been submitted!!!");
     return false;

  // Verify that all the data has been entered 
  if( verifyForm() == false )
    alert ("Please complete the form before you submit it.");
    return false; // Don't submit

  // Set the formSubmitted flag
  formSubmitted = true;

  // Show message to the user
  alert("The form has been submitted.\nThank you!");
  return true; // Submit form


function verifyForm()

  // Use this function to ensure that all the
  // required data has been entered in the form

  // return true if the form is complete
  // return false if its not

  return true;
====================== END Submit.js ============================

This is, of course, only one approach to the problem. You can check
out the following article for other approaches to this

      - Multiple Form Submission Prevention by Will Bontrager 
        ( )

Hope this helps.
If you need any clarifications, or modifications to the code, just


Google Search Terms Used:
     javascript prevent multiple form submissions
cclegg06-ga rated this answer:5 out of 5 stars
Thank you.  Your solution worked fine.

There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy