PHP email form sends email everytime page is refreshed

后端 未结 3 929
半阙折子戏
半阙折子戏 2021-01-17 06:11

my php email for is sending emails every time the page is refreshed. For example the user is filling out the form and sending it with the send button. That\'s all fine and g

3条回答
  •  孤街浪徒
    2021-01-17 07:18

    Use a header instead and make sure you have no output before header.

    if(!$mail->Send()) {
        header("Location: http://www.example.com");
        exit;
        }
    

    If that does not work for you, use a meta refresh method:

    if(!$mail->Send()) {
    $to = "http://www.example.com";
        $url = $to;
        print "";
        exit;
    }
    

    or display a message and redirect after 5 seconds:

    if(!$mail->Send()) {
    $to = "http://www.example.com";
        $url = $to;
        print "";
        print "Thank you for your message.";
        exit;
    }
    

    Edit: (cookie/token method)

    You can use a cookie and this is just an example.

    You can not submit this form twice.
  • '; echo $error_list; echo ' Thank you, your message has been sent. You do not need resubmit it again. '; exit; } foreach( $_POST as $values ) { $data .= $values . "
    "; echo $data; } } ?>
    Name:
    Email:

提交回复
热议问题