所以我正在编写这个订阅页面。。当用户单击提交时,它应该将电子邮件发送到数据库。。我使用的是jQuery的post方法,因此在提交时应该返回send的结果,如果这是真的,则按钮应该消失,并显示一条已发送的消息。但是,当我使用console.log打印结果值时,却没有发生这种情况,它表示成功,但if语句将转到else子句并打印错误消息。
$.post("send_email.php", $("#contact_form").serialize(),function(result){
console.log(result);
//and after the ajax request ends we check the text returned
if(result == "sent"){
//if the mail is sent remove the submit paragraph
$('#button').remove();
//and show the mail success div with fadeIn
$('#mail_success').fadeIn(500);
}else{
console.log('going to failure clause');
//show the mail failed div
$('#mail_fail').fadeIn(500);
//reenable the submit button by removing attribute disabled and change the text back to Send The Message
$('#send_message').removeAttr('disabled').attr('value', 'Submit');
}
});
当我使用console.log打印结果值时,它显示成功
所以,这不是吗:
if(result == "sent"){
需要这样:
if(result == "success"){
我猜您的PHP响应包含空白,当您记录result
时,空白并不明显。如果由于某种原因无法在PHP代码中修复,那么您可以更新JS If条件以忽略空白,如下所示:
if (result.replace(/'s/g,"") === "sent") {