Prevents multiple PHP forms from being delivered

Source: Internet
Author: User
Tags contact form
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:

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.