php - Check which ECHO comes with ajax success -


i have 2 kind of echo in ajax processing script. 1 error messages , other 1 form processing success.

this how look.

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; } 

and other 1 is

// print message based upon result: if ($stmt->affected_rows == 1) {                     // print message , wrap up:     $successmsg  = "<div class='alert alert-success alert-dismissible' role='alert'>\n";     $successmsg .=      "your password has been changed. receive new, temporary password @ email address registered. once have logged in password, may change clicking on 'password modification' link.\n";     $successmsg .= "</div>\n";     echo $successmsg;    } 

so, using 1 div populate these message upon ajax success.

my question is, there way identify message displaying ajax success?

hope may me out. thank you.

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

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

note however, better pattern use return json containing message display, along class of alert , flag indicate state. 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. receive new, temporary password @ email address registered. once have logged in password, may change clicking on password modification link.'); }  echo json_encode($arr); 
// $.ajax({ ... success: function(json) {     if (json.success) {         // worked     }     else {         // went wrong     }      // append alert     $('#myelement').append('<div class="alert alert-dismissible + ' + json.cssclass + '" role="alert">' + json.message + '</div>'); } 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -