Php method to prevent repeated submission of forms

Source: Internet
Author: User
Tags php session

Php method to prevent repeated submission of forms

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

[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 = '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 preceding example, The on click event is used to check 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 the ja vasc ript function, but the on submit () method is used. If a form has been submitted, the dialog box is displayed immediately. The Code is as follows:

[Code] xm l code:

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

02 var submitcount = 0;

03 function submitOnce (form ){

04 if (submitcount = 0 ){

05 submitcount ++;

06 return true;

07} else {

08 alert ("operation in progress. Please do not submit it again. Thank you! ");

09 return false;

10}

11}

12 </SC ript>

13 <form name = "the_form" method = "post" action = "" on submit = "return submitOnce (this)">

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

15 <in put 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:

[Code] php code:

01 <? Php

02 session_start (); // generate a random number based on the current SESSION

03 $ code = mt_rand (usd00 );

04 $ _ SESSION ['code'] = $ code; // Save the random number to the session

05?>

06 <form id = "form1" name = "form1" method = "post" action = "t2.php">

07 <p> description <in put type = "text" name = "titile"/>

08 <in put type = "hidden" name = "originator" value = "<? Php echo $ code;?> "> </P>

09 <p> <in put type = "submit" name = "Submit" value = "submit"/> </p>

10 </form>

Page B:

[Code] php code:

01 <? Php

02session_start ();

03if (isset ($ _ POST ['originator']) {

04 if ($ _ POST ['originator'] = $ _ SESSION ['code']) {

05 echo "OK ";

06 unset ($ _ SESSION ["code"]); // clear it and press F5.

07} else {

08 echo "Please do not refresh this page or submit forms 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.