This is a tricky issue when users submit a form because of the speed of the network or if the Web page is maliciously refreshed, causing the same record to be repeatedly inserted into the database. We can start with the client and server side to try to avoid duplicate submissions of the same form.
1. Using client Script
<form method= "POST" name= "register" action= "test.php" enctype= "Multipart/form-data" >
<input name= "text "Type=" text "id=" text "/> <input name=" cont "value="
Submit "type=" button "onclick=" Document.register.cont.value= ' is submitting, please wait ... ';d ocument.register.cont.disabled=true;document.the_form.submit (); >
</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 submission status of the user, and if the "submit" button is clicked, the button is immediately disabled and the user cannot click the button to submit again.
There is also a way to make use of JavaScript, but using the onsubmit () method, if the form has been submitted once, will immediately pop-up dialog box, the code is as follows:
<script language= "JavaScript" > var submitcount=0; function submitOnce (form) { if (submitcount == 0) { submitcount++; return true; } else{ alert ("in operation, Please do not repeat the submission, thank you! "); return false;
} } </script> <form name= "The_foRM " method=" POST " action=" " onsubmit=" return submitonce (this) > <input name= "Text" type= "text" id= "text" /> <input name= "cont" value= "submitted" type= "Submit" > </form>
In the example above, if the user has clicked the Submit button, the script automatically records the current state and submitcount the variable by 1, and when the user tries to commit again, the script determines that the Submitcount variable value is Non-zero, prompting the user to commit, thus avoiding the repeated submission of the form.
2. Use session (this is the same as the JSP processing method)
Using PHP's session function, you can also avoid submitting forms repeatedly. Session saved on the server side, in the PHP operation can change the session variable, the next access to this variable, the new value is given, so, you can use a session variable record the value of the form submitted, if it does not match, it is the user in the repeated submission.
The code for page A:
<?php session_start (); //generates random numbers $code &NBSP;=&NBSP;MT According to the current session _rand (0,1000000); $_session[' code '] = $code; //this random number to session ?> <form Id= "Form1" name= "Form1" method= "Post" action= "t2.php" > <p> description <input type= "text" name= "Titile" /> <input type= "hidden" name= "originator " value=" <?php echo $code;? > "></p> <p><input type=" Submit " name= "Submit" value= "submitted" /></p> &Nbsp; </form> b page: <?php session_start (); if (isset ($_post[' originator ')) { if ($_post[' originator '] == $_session[' code ') { echo "OK"; unset ($_session["code"]); //remove it and press F5 at this time }else{ echo "Please do not refresh this page or submit the form repeatedly"; } }?>