Php prevents repeated form submission, php prevents form submission
When a user submits a form, the same record may be repeatedly inserted into the database due to the speed of the network or the malicious refreshing of the webpage. This is a tricky problem. We can start with the client and the server to avoid repeated submission of the same form.
1. Use client scripts
[Php]View plaincopy
- <Form method = "post" name = "register" action = "test. php" enctype = "multipart/form-data">
- <Input name = "text" type = "text" id = "text"/>
- <Input name = "cont" value = "Submit" type = "button" onClick = "document. register. cont. value = 'submitting. Please wait... '; document. register. cont. disabled = true; document. the_form.submit (); ">
- </Form>
After you click the submit button, the button becomes unavailable.
In the above example, The OnClick event is used to detect the user's submission status. If you click the submit button, the button is immediately set to invalid. You cannot click the button to submit again.
Another method is to use JavaScript, but the OnSubmit () method is used. If a form has been submitted once, the dialog box is displayed immediately. The Code is as follows:
[Php]View plaincopy
- <Script language = "javascript">
- Var submitcount = 0;
- Function submitOnce (form ){
- If (submitcount = 0 ){
- Submitcount ++;
- Return true;
- } Else {
- Alert ("operation in progress. Please do not submit it again. Thank you! ");
- Return false;
- }
- }
- </Script>
- <Form name = "the_form" method = "post" action = "" onSubmit = "return submitOnce (this)">
- <Input name = "text" type = "text" id = "text"/>
- <Input name = "cont" value = "submit" type = "submit">
- </Form>
In the preceding example, if the user has clicked the submit button, the script automatically records the current status and adds the submitcount variable to 1. When the user tries to submit again, the script determines that the value of the submitcount variable is non-zero and prompts the user that the submitcount variable has been submitted to avoid repeated forms.
2. Use session (this is the same as the JSP processing method)
Using the PHP Session function, you can avoid repeated forms. The Session is stored on the server. During PHP running, you can change the Session variable. The next time you access this variable, you get a new value. Therefore, you can use a Session variable to record the value submitted in the form. If the value does not match, it is considered that the user is submitting the form repeatedly.
Code of page:
[Php]View plaincopy
- <? Php
- Session_start (); // generate a random number based on the current SESSION
- $ Code = mt_rand (usd00 );
- $ _ SESSION ['code'] = $ code; // Save the random number to the session
- ?>
- <Form id = "form1" name = "form1" method = "post" action = "t2.php">
- <P> description <input type = "text" name = "titile"/>
- <Input type = "hidden" name = "originator" value = "<? Php echo $ code;?> "> </P>
- <P> <input type = "submit" name = "Submit" value = "submit"/> </p>
- </Form>
Page B:
[Php]View plaincopy
- <? Php
- Session_start ();
- If (isset ($ _ POST ['originator']) {
- If ($ _ POST ['originator'] = $ _ SESSION ['code']) {
- Echo "OK ";
- Unset ($ _ SESSION ["code"]); // clear it and press F5.
- } Else {
- Echo "Please do not refresh this page or submit the form repeatedly ";
- }
- }?>