I have a JSON data that is returned by AJAX:
PHP file:
//SQL: fetch result if user exist
$result = array("Name"=>$name, "ID"=>$id, "Age"=>$age, "School"=>$school, "Department"=>$dept);
echo json_encode($result);
//else return error
$error = 'No record for this user.';
$result = array("Error"=>$error);
echo json_encode($result);
jQuery AJAX:
//AJAX result
request.done(function( msg ):
//alert( msg ); working fine for users who exist and even those who doesn't exist.
var detail = jQuery.parseJSON( msg );
if ( detail.Name.length > 0 ){
$('.student').slideDown(300);
$('.student').html(detail.Name);
// this is working fine
}
if ( detail.Error.length > 0 ){
$('.student').slideDown(300);
$('.student').html(detail.Error);
//this is not working even if the user does not exist.
}
How do I come around this?
console.log( detail );give you if you call it inside theErrorsegment?echois just once. It echoes once based on the condition that is met: whether a student found in the DB or not.[object Object].console.dir()?