I’m having problems displaying a value from the success function of my Ajax call. My code is as follows.
$.ajax({
type: "POST",
url: "http://localhost/practical 8/checkuser.php",
data: form_data,
success: function(response)
{
if(response == 'success'){
$("#errorUsername").html("<label class='error'>"+response+"</label>");
}else if(response == 'fail'){
$("#errorUsername").html("<label class='error'>"+response+"</label>");
}
}
});
My checkuser.php basically echos “succcess” or “fail”.
My if and else if statements in my success function are not working. But doing
$.ajax({
type: "POST",
url: "http://localhost/practical 8/checkuser.php",
data: form_data,
success: function(response)
{
$("#errorUsername").html("<label class='error'>"+response+"</label>");
}
}
});
works perfectly. What am I doing wrong?
I won’t critique the if branches resulting in the same code and will assume that is just there for testing purposes.
You’ll want to check for extra white space being sent from the PHP script. That would explain why the javascript conditionals don’t return true.
You can also trim the response with jQuery:
response = jQuery.trim(response);