发送邮件php表单后确认提醒

jyztefdp  于 2023-02-28  发布在  PHP
关注(0)|答案(1)|浏览(118)
<?php

//PHPMailer first part of the code

try {
   
//PHPMailer second part of the code

    $mail->send();
    
    header('Location: contacto.php');

    $message = "Mail sent";

    echo "<script type='text/javascript'>alert('$message');</script>";

} catch (Exception $e) {
    echo "Error al enviar: {$mail->ErrorInfo}";
    
    header('Location: contacto.php');

    $message1 = "Mail not sent";

    echo "<script type='text/javascript'>alert('$message1');</script>";
}

如何在phpmailer表单联系人发送邮件后显示一个提醒框?我的提醒没有重定向头就不起作用.如果我设置了头(重定向用户到联系人页面),我的提醒就不起作用.

zed5wv10

zed5wv101#

header location语句只是将您重定向到所需的页面,因此有效地终止了当前PHP脚本,因此不会触发alert语句。
其中一种方式是触发告警,然后进行重定向(都是通过JS)
因此代码可以是:

<?php

//PHPMailer first part of the code

try {
   
//PHPMailer second part of the code

    $mail->send();
    
//    header('Location: contacto.php');

    $message = "Mail sent";

    echo "<script type='text/javascript'>alert('$message');</script>";
    echo "<script>window.location.href='contacto.php';</script>";

} catch (Exception $e) {
    echo "Error al enviar: {$mail->ErrorInfo}";
    
//    header('Location: contacto.php');

    $message1 = "Mail not sent";

    echo "<script type='text/javascript'>alert('$message1');</script>";
    echo "<script>window.location.href='contacto.php';</script>";

}

相关问题