0

所以我正在写这个订阅页面..当用户点击提交时,它应该将电子邮件发送到数据库..我使用 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');
    }
});
4

2 回答 2

2

当我使用 console.log 打印结果值时,它说成功

所以,这不是:

                    if(result == "sent"){

需要是这样的:

                    if(result == "success"){

?

于 2012-07-15T04:15:35.727 回答
1

我的猜测是您的 PHP 响应包含登录时不明显的空格result。如果由于某种原因无法在 PHP 代码中修复,那么您可以更新您的 JS if 条件以忽略空格,如下所示:

if (result.replace(/\s/g,"") === "sent") {
于 2012-07-15T04:56:38.277 回答