Php forms developed for automatic submission of two problem solutions, php forms
Preface:
Recently developed. On the PHP page accessed in, there is a form on the page. The test finds that the form is automatically submitted twice, resulting in a process error.
Cause
No reason is found for the moment, but it is suspected that it is related to its own mechanism.
Solution
With session, a random number post is calculated every time a form is submitted. When submitting a page Processing Form, first determine whether the session exists. If the session does not exist, save the session. If the session exists, it is compared with the post random number. If the session is equal, it indicates repeated submission.
Sample Code:
Form part
<Div class = "odform"> <form action = "#" method = "post"> <div class = "input-group"> <label for = "amount_id"> amount </label> <input type = "text" name = "amount" id = "amount_id" placeholder = "unit: RMB, accurate to score, for example: 12.66 "pattern =" ^ ([1-9] + (\. [0-9] {2 })? | 0 \. [1-9] [0-9] | 0 \. 0 [1-9]) $ "> <input type =" hidden "name =" code "value =" <? Php $ code = mt_rand (0, 100000); echo $ code;?> "> </Div> <div class =" input-group "> <span id =" available "> current balance: <? Php echo $ amount_available;?> Meta </span> </div> <button name = "confirm"> OK </button> </form> </div>
Process Form submission
If (isset ($ _ POST ['Confirm']) {// solve the problem of repeated calls twice if ($ _ POST ['code'] ==$ _ SESSION ['code']) {exit ;} else {$ _ SESSION ['code'] = $ _ POST ['code'];} // continue ....}
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!