gpt4 book ai didi

php - 在 phpmailer 中尝试并捕获

转载 作者:行者123 更新时间:2023-12-01 04:03:23 26 4
gpt4 key购买 nike

我有一个 html 表单。当用户按下 sumbit 时,jQuery 会向 php 脚本 (send.php) 发送包含数据的帖子。在 send.php 文件中,我创建了一个 php 对象:

 $mail = new PHPMailer(true);

然后我尝试一下:

    try {
$mail->IsSMTP();

// ........ code for host, usr/psw, body, header ........


$mail->Send();

echo "Ok, Sent!\n";

} catch (phpmailerException $e) {
echo $e->errorMessage();
}

它给我发送了邮件,但我不知道消息在哪里 echo "Ok, Sent!\n"; ...它在哪里?我在任何地方都看不到它。应该在哪里?我希望显示在提交 button 下,例如,如果输入不正确,它可能会显示此内容:

incorrect data

如果它正在发送电子邮件

sending mail

如果发送正确的话:

Ok, Sent!

我该怎么办?

更新这是index.html页面中的jQuery代码:

    $('form').submit(function() { 

var event_data = encodeURIComponent( $("#event").text() );

var formID = $(this).attr('id');
var formDetails = $('#'+formID);
$.ajax({
type: "POST",
url: 'send.php',
data: formDetails.serialize() + "&event=" + event_data,
success: function (data) {
},
error: function(jqXHR, text, error){
$('#result').html(error);
}
});
return false;
});

最佳答案

将 AJAX 代码更改为如下所示:

$('form').submit(function() { 

var event_data = encodeURIComponent( $("#event").text() );

var formID = $(this).attr('id');
var formDetails = $('#'+formID);
$('#result').html('sending...'); // changing the text before sending mail...
$.ajax({
type: "POST",
url: 'send.php',
data: formDetails.serialize() + "&event=" + event_data,
success: function (data) {
$('#result').html(data); // this will show the output of the page send.php
console.log(data); //this will also show the output of the page in the browser console.
},
error: function(jqXHR, text, error){
$('#result').html(error);
}
});
return false;
});

send.php像这样:

try {
$mail->IsSMTP();

// ........ code for host, usr/psw, body, header ........


if($mail->Send()){
echo "Ok, Sent!\n";
}

} catch (phpmailerException $e) {
echo $e->errorMessage();
}

关于php - 在 phpmailer 中尝试并捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35335744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com