开发者

Form redirection problem [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to stop form from sending email more times after initial success.

First of all, i have structured my website using directories. So basically every page is a directory and in that directory, i have a file called index.php.

I have four contact forms on my site, and at the moment, the seem to all work using the hnadler.php file. The handler file validates the data, checks the form-id posted and based on that, it routes the email appropraitely. A success message is displayed if successfully sent. However, my current implimentation is flawed in that if the user refreshes, another mail is sent. How can i solve this with my existing code? Thank you

//handler.php   
<?php
    if(isset($_POST['submit'])) {
    //carry out validation

    if(!isset($hasError)) {
        //check the form id posted and set email address in $emailTo accordingly

        $body = "Name: $name \n\nEmail: $email \n\nEnquiry: $enquiry";
        $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

        mail($emailTo, $subject, $body, $headers);
        $emailSent = true;        
    }
}

//index.php
<?php if(isset($hasError)) { ?> 
    <p class="error">Please make sure you have filled all fields with valid information. Thank you.</p>
<?php } ?>                        
<?php if(isset($emailSent) && $emailSent == true) { ?>
    <p><strong>Your enquiry was sent successfully.</strong></p>
    <p>Thank you for your enquiry! Your email was successfully sent and we will be in touch with you promptly.</p>
<?php }; ?>                       
<form id="contactform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <fieldset>
        <legend>Enquiry 开发者_开发技巧form</legend>
        <label for="name">Name:</label><input type="text" size="50" name="name" id="name" value="" class="required" />
        <label for="email">Email:</label><input type="text" size="50" name="email" id="email" value="" class="required email" />
        <label for="enquiry">Enquiry:</label><textarea rows="5" cols="20" name="enquiry" id="enquiry" class="required"></textarea>
        <input type="submit" name="submit" value="Submit enquiry" class="curved-btn"></input>
        <input type="hidden" id="form-id" name="form-id" value="general"></input>
    </fieldset>
</form>

?>


The problem is that you aren't actually submitting to handler.php because of this:

<form id="contactform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Change it to this:

<form id="contactform" method="post" action="handler.php">

And put your send e-mail code inside of handler.php. You will also need to put a redirect in there to get them back to a page. There are other ways to go about this, but this is how I would do it.


The browser will/can repost the data on refresh, so it will look like a new request.

A quick fix is to redirect after the form submission:

header("Location: success.php");

That way if the refresh they refresh the success page, not the page you posted to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜