检查哪个ECHO带有ajax成功

时间:2015-07-02 08:05:06

标签: php jquery ajax

我的ajax处理脚本中有两种echo。一个用于错误消息,另一个用于表单处理成功。

这是它的外观。

if (strlen($password) != 128) {
        $errorMsg  = "<div class='alert alert-danger alert-dismissible' role='alert'>\n";
        $errorMsg .=        "<strong>Oops!</strong> System error, Invalid password configuration.\n";
        $errorMsg .= "</div>\n";
        echo $errorMsg;
}

另一个是

// Print a message based upon the result:
if ($stmt->affected_rows == 1) {                
    // Print a message and wrap up:
    $successMsg  = "<div class='alert alert-success alert-dismissible' role='alert'>\n";
    $successMsg .=      "Your password has been changed. You will receive the new, temporary password at the email address with which you registered. Once you have logged in with this password, you may change it by clicking on the 'Password Modification' link.\n";
    $successMsg .= "</div>\n";
    echo $successMsg;   
}

所以,我使用一个DIV在ajax成功时填充这些消息。

我的问题是,有没有办法确定哪个消息显示ajax成功?

希望有人可以帮助我。 谢谢。

1 个答案:

答案 0 :(得分:3)

您可以使用/restricted-area查看回复是否具有/restricted类:

filter()

但请注意,更好的模式是返回包含要显示的消息的JSON,以及警报的类和指示其状态的标志。像这样:

.alert-danger
// $.ajax({ ...
success: function(html) {
    var $html = $(html);
    if ($html.filter('.alert-danger').length) {
        // something went wrong
    }
    else {
        // it worked
    }
}