Search code examples
phphtmlphpmailer

How to stop phpmailer multiple resend on refresh? (and show error message)


When i refresh the page the phpmailer always resends the email. What i did? Used the header("Location: home.php");

But how can i do the Location to home.php and show my error message $error = "Thank you for message!";

if($mail->send()){
    header("Location: home.php");
    $error = "Thank you for message!";
} else {
    $error .= "Error {$mail->ErrorInfo}";
}

the problem is that when i do the header it does not show me the error message...

<div class="text-center impact">
<?php echo $error; ?>
</div>

Solution

  • You can pass a GET-parameter so you can check it when the page reloads. Try this code example:

    if($mail->send()){
        header("Location: home.php?success");
    } else {
        $error .= "Error {$mail->ErrorInfo}";
    } 
    

    And on your page:

    <div class="text-center impact">
    <?php echo isset($_GET['success']) ? "Thank you for message!" : $error; ?>
    </div>