Prevent multiple PHP forms from being delivered

Source: Internet
Author: User
Tags php form setcookie contact form
Prevent multiple PHP form submissions
When an HTML form is submitted, it may take a few seconds before the submission is successful and the form of the response page is displayed. People can get free and click the Submit button several times, which may lead to duplicate form submissions. Usually not really a problem, but in some cases you may want to prevent this from happening.

Below you will find two simple tricks to prevent duplicate submissions that you can use to combine these or both.

? Prevent multiple forms from being submitted using Javascript

Repeating submissions using JavaScript blocks can be the simplest method. When someone submits a form we simply disable the submit button and may change its value to something more descriptive, such as "Submit, please wait ..."

Try clicking on this button, for example. It will still be disabled until you reload this page:

The first step is to give your submit button a unique ID, such as id = "MyButton":

The second (and last) step is to give two JavaScript commands

Mark. The first one will tell the browser to disable the Submit button after the form is submitted, the second will change the text of the button to give the user some idea what's going on. This is what the code adds to your form tag:

onsubmit= "document.getElementById (' MyButton '). Disabled=true;document.getelementbyid (' MyButton '). Value= ' Submitting, please wait ... ';

Your form tag will look similar to the following:

That's it. This method should be used in most browsers (IE +,firefox, Opera 、...).

? Prevent multiple forms from being submitted using cookies

If you want to avoid repeating submissions for the entire browser session (or longer), you can consider using cookies. For example, the browser that edits its own form processing script sends a cookie form after it has been processed, but before any HTML or redirected print header is taken. After the Mail () command for this piece of code, it should be in most cases:

Setcookie (' formsubmitted ', ' 1 ');

Then check the cookie before processing. If the visitor has already submitted the form in the active browser session. Add the following code to the start processing script for the form:

if (Isset ($_cookie[' formsubmitted ')) {die    (' could only submit this form once per session! ');}

That's it!
If modified, to prevent duplicate submissions with cookies. Note: Add some code to the mail () function after the print code of the above script and cookie is called:

      /* Prevent Duplicate Submissions */if (isset ($_cookie[' formsubmitted ']) {show_error (' You may only submit this form once Per session! ');}/* Set e-Mail Recipient */$myemail = "[email protected]";/* Check all form inputs using check_input function */$your Name = Check_input ($_post[' yourname '), "Enter Your Name"), $subject = Check_input ($_post[' subject '], "Write a Subject"); $ email = check_input ($_post[' email '), $website = check_input ($_post[' website ')); $likeit = Check_input ($_post[' Likeit ') ]); $how _find = check_input ($_post[' how '); $comments = Check_input ($_post[' comments '], "Write your Comments");// www.software8.co Original */If e-mail is not valid show error message */if (!preg_match ("/([\w\-]+\@[\w\-]+\.[ \w\-]+)/", $email)) {show_error (" e-mail address not valid ");} /* If URL is not valid set $website to empty */if (!preg_match ("/^ (https?:\ /\/+[\w\-]+\. [\w\-]+)/I ", $website)) {$website = ';} /* Let's prepare the message for the e-mail */$message = "hello! Your Contact Form has been submitted by:name: $yournameE-mail: $emailURL: $websiteLike the website? $likeitHow did he/she find it? $how _findcomments: $commentsEnd of message ";/* Send the message using the Mail () function */mail ($myemail, $subject, $message); /* Set a cookie to prevent duplicate submissions */setcookie (' formsubmitted ', ' 1 ');/* Redirect visitor to the Thank you page */header (' Location:thanks.htm '); exit ();/* Functions we used */function check_in    Put ($data, $problem = ') {$data = Trim ($data);    $data = Stripslashes ($data);    $data = Htmlspecialchars ($data);    if ($problem && strlen ($data) = = 0) {show_error ($problem); } return $data;} function Show_error ($myError) {?>        Please correct the following error:
  • Related Article

    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.