Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

Safe Submit Button

5.00/5 (7 votes)
5 May 2013CPOL 24.1K  
How to send data from HTML form to server safely

Introduction

When a user on web form presses "Submit" button for sending data to web server, then the web server runs a script, query, etc. This may take uncertain time up to 1 minute. But the user sees a static page and may think: "I did not press the button or something is wrong" and press the button again many times. So the web server may get many HTML queries. This will increase CPU usage on the server and may return incorrect results.

Background

The idea is simple.

  • Step 0 (may skip): Check the form: run function "OnSubmit" in tag FORM, check tag INPUT ("required" and "pattern" attributes).
  • Step 1: Change text on the button to "Please wait...".
  • Step 2: Disable the button.
  • Step 3: Send query.

Using the Code

Just add code to input tag:

HTML
    <input type="submit" name="Submit" value="Submit"  
önclick = "this.form.check(); this.value='Please wait...'; this.disabled=true; this.form.submit();" />    

So see 'on click' event.

Step 0. Check the Form

JavaScript
this.form.check();

Step 1. Change Text on the Button

JavaScript
this.value='Please wait...';

Step 2. Disable the Button

JavaScript
this.disabled=true;

Step 3. Press the Button Programmatically

JavaScript
this.form.submit();

Points of Interest

See also:

History

  • 1.1 | Added Step 0: Check form | 06.05.2013

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)