PHP prevents form repeat submission method

Source: Internet
Author: User

This is a tricky issue when users submit a form because of the speed of the network or if the Web page is maliciously refreshed, causing the same record to be repeatedly inserted into the database. We can start with the client and server side to try to avoid duplicate submissions of the same form.

1. Using Client Script

[Code]XM L Code:

1<form method= "POST" name= "register" action= "test.php" enctype= "Multipart/form-data" >

2<in put name= "text" type= "text" id= "text"/>

3<in put Name= "cont" value= "Submit" type= "button" on click= "Document.register.cont.value=" is being submitted, please wait ... '; Document.register.cont.disabled=true;document.the_form.submit (); " >

4</form>

When the user clicks the Submit button, the button changes to a gray unavailable state

The above example uses the On Click event to detect a user's commit status, and if the Submit button is clicked, the button is immediately disabled and the user cannot click the button to submit again.

There is also a way to take advantage of the function of the JA vasc ript, but using the on Submit () method, if the form has been submitted once, the dialog will be immediately pop-up, the code is as follows:

[Code]XM L Code:

01<SC ript language= "ja vasc ript" >

var submitcount=0;

The function submitonce (form) {

if (Submitcount = 0) {

submitcount++;

return true;

Modified else{

("In operation, please do not repeat the submission, thank you!");

return false;

10}

11}

</SC ript>

<form name= "The_form" method= "POST" action= "on submit=" return submitonce (This) ">

<in put name= "text" type= "text" id= "text"/>

<in put Name= "cont" value= "submitted" type= "Submit" >

</form>

In the example above, if the user has clicked the Submit button, the script automatically records the current state and submitcount the variable by 1, and when the user tries to commit again, the script determines that the Submitcount variable value is Non-zero, prompting the user to commit, thus avoiding the repeated submission of the form.

2. Use session (this is the same as the JSP processing method)

Using PHP's session function, you can also avoid submitting forms repeatedly. Session saved on the server side, in the PHP operation can change the session variable, the next access to this variable, the new value is given, so, you can use a session variable record the value of the form submitted, if it does not match, it is the user in the repeated submission.

The code for page A:

[Code]php Code:

01<?php

Session_Start (); Generate random numbers based on current session

$code = Mt_rand (0,1000000);

$_session[' Code ' = $code; Save this random number to session

?>

<form id= "Form1" Name= "Form1" method= "Post" action= "t2.php" >

<p> notes <in put type= "text" name= "Titile"/>

<in put type= "hidden" name= "originator" value= "<?php echo $code;? > "></p>

<p><in put type= "submit" name= "Submission" value= "submitted"/></p>

Ten </form>

b page:

[Code]php Code:

01<?php

02session_start ();

03if (Isset ($_post[' originator ')) {

if ($_post[' originator '] = = $_session[' Code ']) {

echo "OK";

unset ($_session["Code"]); Clear it off and press F5 at this point is not valid

Modified}else{

"Please do not refresh this page or submit the form repeatedly";

09}

10}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.