检查哪个ECHO带有ajax成功

I do have two kind of echo in my ajax processing script. One for error messages and other one for form processing success.

This is how its look.

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

And other one is

// 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'>
";
    $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.
";
    $successMsg .= "</div>
";
    echo $successMsg;   
}

So, I am using one DIV to populate these message upon the ajax success.

My question is, is there a way to identify which message is displaying with ajax success?

Hope somebody may help me out. Thank you.

You can use filter() to see if the response has the class of .alert-danger:

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

Note however, that a better pattern to use would be to return JSON containing the message to display, along with the class of the alert and a flag to indicate its state. Something like this:

var $arr;

if (strlen($password) != 128) {
    $arr = array('success'=>false,'cssClass'=>'alert-danger','message'=>'Oops! System error, Invalid password configuration.');
}

if ($stmt->affected_rows == 1) {
    $arr = array('success'=>true,'cssClass'=>'alert-success','message'=>'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.');
}

echo json_encode($arr);
// $.ajax({ ...
success: function(json) {
    if (json.success) {
        // it worked
    }
    else {
        // something went wrong
    }

    // append the alert
    $('#myElement').append('<div class="alert alert-dismissible + ' + json.cssClass + '" role="alert">' + json.message + '</div>');
}