PHP prevents forms from repeating commits

Source: Internet
Author: User
Tags vars

It is a tricky problem for users to submit a form because of the speed of the network, or if the webpage is maliciously flushed, causing the same record to be repeatedly inserted into the database. We can start with the client and the server side, and try to avoid repeating submissions for the same form.

1. Using client Script

[PHP]View Plaincopy
    1. <form method= "POST"  name= "register"  action= "test.php"  enctype= " Multipart/form-data ">&NBSP;&NBSP;
    2. <input name= "text"  type= "text"  id= "text"  / >  
    3. <input name= "cont"  value= "commit"  type= "document.register.cont.value=" is being submitted, please wait ... ';d ocument.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 OnClick event to detect the user's submission status, and if the Submit button is clicked, the button is immediately invalidated and the user cannot click the button to submit again.

There is also a way to take advantage of JavaScript, but using the onsubmit () method, if you have submitted a form once, the dialog box will immediately pop up with the following code:

[PHP]View Plaincopy
  1. <script language="JavaScript" >
  2. var submitcount=0;
  3. function Submitonce (form) {
  4. if (Submitcount = = 0) {
  5. submitcount++;
  6. return true;
  7. } else{
  8. Alert ("is operating, please do not repeat the submission, thank you!")  ");
  9. return false;
  10. }
  11. }
  12. </script>
  13. <form name="The_form" method="post" action="onsubmit="return submitonce (This) ">
  14. <input name="text" type="text" id="text"/>
  15. <input name="cont" value="Submission" type="Submit" >
  16. </form>




In the example above, if the user has clicked the Submit button, the script automatically records the current state and adds the Submitcount variable to 1, and when the user tries to commit again, the script determines that the value of the Submitcount variable is nonzero, prompting the user to submit it, thereby avoiding repeating the form.


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

You can also avoid repeating the form by using the session function of PHP. Session is saved on the server side, in the PHP run process can change the session variable, the next time you access this variable, get a new assignment value, so, you can use a session variable to record the value of the form submission, if not match, it is considered that the user is repeating the commit.
The code for the A page:

[PHP]View Plaincopy
  1. <?php
  2. Session_Start (); generate random numbers based on current session
  3. $code = Mt_rand (0,1000000);
  4. $_session[' Code ' = $code; //Put this random number into session
  5. ?>
  6. <form id="Form1" Name="Form1" method="post" action="t2.php" >
  7. <p> description <input type="text" name="Titile"/>
  8. <input type="hidden" name="originator" value= "<?phpecho $code;? > "></p>
  9. <p><input type="Submit" Name="submit" value="Submission"/></p>
  10. </form>



b page:

[PHP]View Plaincopy
    1. <?php
    2. Session_Start ();
    3. if (Isset ($_post[' originator ')) {
    4. if ($_post[' originator '] = = $_session[' code ')} {
    5. echo "OK";
    6. unset ($_session["Code"]); //Clear it out at this point and then press F5 to void
    7. }else{
    8. echo "Please do not refresh this page or submit the form again";
    9. }
    10. }?>

PHP prevents forms from repeating commits

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.