Pavlos1316
Pavlos1316

Reputation: 444

Ajax success responce

I have the below code which is working fine:

success: function (upd_rsp){
  //if email is not register
  if(upd_rsp == "email_no"){
  exstInfo.text("This E-mail is not registered in our Database!");
  exstInfo.addClass("error");
  return false;
  }

  //if email & pass don't match
  if(upd_rsp == "email_pass_no"){
  matchInfo.text("Email and Password don't match!");
  matchInfo.addClass("error");
  return false;
  }

Now if I put between those 2 "if" an "else" statement (below), the second "if" does not get executed. Where am I wrong?

   else{
   exstInfo.text("");
   exstInfo.removeClass("error");
   return true;
   }

Solution works just fine.

Upvotes: 0

Views: 63

Answers (1)

johnmadrak
johnmadrak

Reputation: 825

If you set a return value before the second if is executed, the script will stop once the return is sent.

This is one of the many ways you could do it:

success: function (upd_rsp){
  //if email is not register
  var email_error = false;
  var pass_error = false;
  if(upd_rsp == "email_no"){
    exstInfo.text("This E-mail is not registered in our Database!");
    exstInfo.addClass("error");
    email_error = true;
  } else {
    exstInfo.text("");
    exstInfo.removeClass("error");
    email_error = false;
  }


  //if email & pass don't match
  if(upd_rsp == "email_pass_no"){
    matchInfo.text("Email and Password don't match!");
    matchInfo.addClass("error");
    pass_error = true;
  } else {
    ....
    pass_error = false;
  }

  if(email_error == true || pass_error == true){
    return false;
  } else {
    return true;
  }

Upvotes: 2

Related Questions