Home  >  Article  >  Web Front-end  >  Common form duplicate submission problems and solutions_javascript skills

Common form duplicate submission problems and solutions_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:15:411032browse

/**
*
* @authors Benjamin
* @date 2013-11-13 10:16:59
*/

1. Common duplicate submission issues
a>Click the submit button twice.
b>Click the refresh button.
c>Use the browser back button to repeat the previous operation, resulting in repeated submission of the form.
d> Repeat form submissions using browser history.
e> Duplicate HTTP requests from the browser.

2. Methods to prevent repeated submission of forms
 a> Disable the submit button. After the form is submitted, disable the current button or cancel the click event or default event of the button. This approach prevents impatient users from clicking the button multiple times. But there is a problem. If Javascript is disabled on the client, this method will be ineffective. Of course, for modern web sites, it should be rare.

b>Post/Redirect/Get mode. Performing page redirection after submission is the so-called Post-Redirect-Get (PRG) pattern. In short, when the user submits the form, you perform a client-side redirection and go to the submission success information page. This can avoid repeated submissions caused by the user pressing F5, and there will be no warning about repeated submissions of the browser form. It can also eliminate the same problems caused by pressing the forward and backward buttons of the browser.

c> Use cookies to handle repeated form submissions
Implementation in PHP:

Copy code The code is as follows :

 lt;?php
  if(isset($_POST['go'])){
  setcookie("tempcookie","",time() 30);
header("Location:".$_SERVER[PHP_SELF]);exit();
} if(isset($_COOKIE["tempcookie"])){
setcookie("tempcookie","", 0);echo "You have already submitted the form";
  }
  ?>

 d> Store a special flag in the session. When the form page is requested, a special character string is generated, stored in the session, and placed in the hidden field of the form. When accepting and processing form data, check whether the identification string exists, immediately delete it from the session, and then process the data normally. If it is found that there is no valid flag string in the form submission, it means that the form has already been submitted and this submission is ignored. This gives your web application more advanced XSRF protection.

e>Add constraints in the database. Add unique constraints or create unique indexes in the database to prevent duplicate data. This is the most effective way to prevent duplicate submission of data.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn