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

问题描述 投票:0回答:1
<?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 表单联系人发送邮件后显示警告框?我的警报在没有重定向标头的情况下也能正常工作。如果我设置标题(将用户重定向到联系页面)我的警报不起作用。

php forms alert contacts
1个回答
0
投票

标头位置语句只是将您重定向到您想要的页面,因此有效地终止了当前的 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>";

}
© www.soinside.com 2019 - 2024. All rights reserved.