Click here to Skip to main content
15,867,488 members
Articles / Web Development / HTML
Tip/Trick

Safe "Submit" Button

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
5 May 2013CPOL 23.2K   12   5
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)


Written By
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionClient side validation leaves button disabled Pin
Robert J. Good8-May-13 4:30
professionalRobert J. Good8-May-13 4:30 
QuestionYou can do with PRG Pattern easily Pin
Ravi Gadag5-May-13 22:55
Ravi Gadag5-May-13 22:55 
GeneralMy vote of 5 Pin
M Rayhan1-May-13 1:45
M Rayhan1-May-13 1:45 
GeneralMy vote of 5 Pin
JayantaChatterjee25-Apr-13 2:39
professionalJayantaChatterjee25-Apr-13 2:39 
GeneralMy vote of 5 Pin
dgDavidGreene24-Apr-13 9:26
dgDavidGreene24-Apr-13 9:26 
Simple and clever

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.