Prevent multiple PHP forms from being submitted. when submitting an HTML form, it may take several seconds before it is successfully submitted and the response page is displayed. People can get idle and click the submit button several times, which may lead to repeated form submission. Generally, this is not really a problem, but in some cases, you may want to prevent it from happening. Below you will prevent multiple PHP forms from being submitted
When submitting an HTML form, it may take several seconds before it is successfully submitted and the form of the response page is displayed. People can get idle and click the submit button several times, which may lead to repeated form submission. Generally, this is not really a problem, but in some cases, you may want to prevent it from happening. Here are two simple tips to prevent repeated submissions. you can use these or both.
? Prevents multiple forms from being submittedJavascript
Repeated submission using Javascript blocks may be the simplest method. When someone submits a form, we simply disable the Submit button, which may change its value to something more descriptive, such as "Submit, please wait ......"
Click this button, for example. It will still be disabled until you reload the page:
The first step is to give your submit button a unique id, such as id = "myButton":
The second (and the last) step is to give two Javascript commands
Mark. The first one will tell the browser that the form for disabling the submit button is submitted, and the second one will change the button text to give users some information about what happened. Here is the form tag added to your code:
onsubmit="document.getElementById('myButton').disabled=true;document.getElementById('myButton').value='Submitting, please wait...';" |
Your form tag will be similar:
That's it. This method should be applied to most browsers (IE +, FireFox, Opera ,...).
? Prevent multiple forms from being submitted using cookies
If you want to avoid repeatedly submitting the entire browser session (or longer), you can consider using cookies. For example, after the browser that edits its form processing script sends the cookie form, it has been processed, but any HTML or redirection print header prior to this is. After the mail () command of this code is run, it should be in most cases:
setcookie('FormSubmitted', '1'); |
Then, check the cookie before processing. If a visitor has submitted a form in an active browser session. Add the following code to the start processing script of the form:
if (isset($_COOKIE['FormSubmitted']){ die('You may only submit this form once per session!');} |
That's it!
If modified, to prevent repeated submission and cookies. Note: after the preceding script and cookie printing code are called, add the mail () function of some code:
Please correct the following error:
|