Prevent php form submission Refresh Auto Repeat submit

Source: Internet
Author: User
Tags commit php code php form setcookie valid what php

JS Block Duplicate Commit


The first is when the form that tells the browser to disable the Submit button is submitted, and the second is to change the text of the button to give the user some idea of what's going on. This is the code added to your form tag: onsubmit= "document.getElementById" (' MyButton '). Disabled=true;document.getelementbyid (' MyButton '). Value= ' submitting, please wait ... '; Your form tags will resemble:

or jquery approach

$ (document). Ready (function () {
$ (input:submit). Click () {
settimeout (function () {obj.disabled=true;},100)
};
});

This is what PHP programmers do to prevent users from submitting multiple forms, and this approach is applied to most browsers (IE +,firefox, Opera 、...).

Session Block Repeat Commit

Because the contents of a form variable are referenced by $_post[' name ', it may be possible to destroy the $_post[' name ' (unset () directly after the form has been processed), but it may not be because the page defaults to caching the contents of the form, so even if you destroy the $_post[' Name '], and the $_post[' name ' will be assigned as valid after the refresh.

can be resolved by session. First give the session a value, such as 400, the first time after the successful change session value, when the second submission to check the session value, if not 400, no longer processing the data in the form. Can I set a valid time for the session?

<?php
if (Isset ($_post[' action ')) && $_post[' action '] = = ' submitted ') {
Session_Start ();
Isset ($_session[' num ']) or Die ("no Session");
if ($_session[' num ']==400) {

Echo ' <a href= '. $_server[php_self]. >please try Again</a> ';
$_session[' num ']=500;
} else {

echo "However you have submitted";
}
} else {
Session_Start () or Die ("session isn't started");
$_session[' num ']= 400;
?>
<form action= "<?php echo $_server[' php_self ';?>" method= "POST" >
Name: <input type= "text" Name= "Personal[name]" ><br>
Email: <input type= "text" Name= "Personal[email]" ><br>
Beer: <br>
<select multiple Name= "beer[]" >
<option value= "Warthog" >Warthog</option>
<option value= "Guinness" >Guinness</option>
<option value= "Stuttgarter" >stuttgarter schwabenbr</option>
</select><br>
<input type= "hidden" name= "action" value= "submitted" >
<input type= "Submit" name= "submit" value= "Submit me!" >
</form>
<?php
}
?>

Cookies Prevent duplicate Submissions

Introduce a cookie mechanism to resolve (this method is not recommended, the reason will be written later)

Submit the page code as follows a.php code as follows:

<form id= "Form1" Name= "Form1" method= "Post" action= "b.php" >
<p> description
<input type= "text" name= "Titile"/>
</p>
<p>
<input type= "Submit" name= "Submission" value= "submitted"/>
</p>
</form>
<?php
Setcookie ("Onlypost", ' t '); Set the cookie to bring the time value. Like some of the forums to prevent irrigation can put some of your basic information into the inside.
?>

The processing page b.php code is as follows:

<?php
if ($_cookie[' onlypost '] = = ' t ') {
Printr ($COOKIE);
Processing of committed content if validation succeeds
print "OK";
Setcookie ("Onlypost", ' f '); Change the cooike value to delete it too
}
?>

Use header function to jump

Once the user clicks on the Submit button, the data is processed and jumps to another page

if (Isset ($_post[' submit ')) {
Header (' location:success.php ');//After processing data, move to another page
}

Using a database to add constraints

Add a unique constraint directly to the database or create a unique index, and once you find that the user has repeatedly committed, throw a warning or prompt,
Or only the first submission of data, this is the most direct and effective way to require early database design and architecture to be considered comprehensive

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.