Home » Javascript » If statement firing wrong alert

If statement firing wrong alert

Posted by: admin June 30, 2018 Leave a comment

Questions:

I am trying to fire an alert in success function of ajax call. The problem is that it keeps firing the same alert and doesn’t seem to recognize the data string value.

In my example, the alert that keep firing is the yes alert. I can see in firebug that the value being returned by php is what I am trying to compare against. I am confused as to why this simple function is not working and need to find solution before I start main coding. I would be grateful if someone could help with this. Many thanks

ajax

success: function(data) {
  if (data == 'No Date Set') {
    alert('No');
  }
  else {
    alert('yes');
  }
}

php

if($ddate =='No Date Set') {
  echo $ddate;
}

ajax code snippet

$.ajax({
  type: 'POST',
  url: 'niajax.php',
  data: data,
  async: false,
  beforeSend: function() {
    $(".text").show().html('Processing your request...');
  },
  success: function(data) {
    console.log(data);
    if (data == 'No Date Set') {
      alert('Date not set');
    }
    else {
      alert('Date set');
    }
  },
  error: function(xhr) {
    $(".text").fadeIn(4000).html('Error occured.please try again').fadeOut(4000);
    //alert("Error occured.please try again");
  },
  complete: function() {},
})
Answers: