Php prevents repeated form submission, php prevents form submission

Source: Internet
Author: User

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
  1. <Form method = "post" name = "register" action = "test. php" enctype = "multipart/form-data">
  2. <Input name = "text" type = "text" id = "text"/>
  3. <Input name = "cont" value = "Submit" type = "button" onClick = "document. register. cont. value = 'submitting. Please wait... '; document. register. cont. disabled = true; document. the_form.submit (); ">
  4. </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
  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 ("operation in progress. Please do not submit it again. 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 = "submit" type = "submit">
  16. </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
  1. <? Php
  2. Session_start (); // generate a random number based on the current SESSION
  3. $ Code = mt_rand (usd00 );
  4. $ _ SESSION ['code'] = $ code; // Save the random number to the 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 = "<? Php echo $ code;?> "> </P>
  9. <P> <input type = "submit" name = "Submit" value = "submit"/> </p>
  10. </Form>



Page B:

[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 and press F5.
    7. } Else {
    8. Echo "Please do not refresh this page or submit the form repeatedly ";
    9. }
    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.