Prevent multiple PHP forms from submitting _ PHP tutorials

Source: Internet
Author: User
Tags contact form
Prevents 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. This may lead to repeated submission of an HTML form. it may take several seconds to submit the form and display the response page form. 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.
» Prevent multiple forms from being submitted using Javascript
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 ('myclick'). disabled = true;
Document. getElementById ('myclick'). value = 'submitting, please wait ...';"
Your form tag will be similar:
Onsubmit = "document. getElementById ('myclick'). disabled = true;
Document. getElementById ('myclick'). value = 'submitting, please wait ...';"
>
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:
/* Prevent duplicate submissions */
If (isset ($ _ COOKIE ['formsubmitted'])
{
Show_error ('you may only submit this form once per session! ');
}

/* Set e-mail recipient */
$ Myemail = "you@domain.com ";

/* Check all form inputs using check_input function */
$ Yourname = check_input ($ _ POST ['yourname'], "Enter your name ");
$ Subject = check_input ($ _ POST ['subobject'], "Write a subject ");
$ Email = check_input ($ _ POST ['email ']);
$ Website = check_input ($ _ POST ['Website']);
$ Likeit = check_input ($ _ POST ['likeit']);
$ How_find = check_input ($ _ POST ['who']);
$ 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:

Name: $ yourname
Email: $ email
URL: $ website

Like the website? $ Likeit
How did he/she find it? $ How_find

Comments:
$ Comments

End of message
";

/* Send the message using 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_input ($ 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:



Exit ();
}
?

Bytes. People can get idle and click the submit button several times, which may lead to repetition...

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.