I have this php that returns an array if an error is triggered, else, it returns a <DIV>
if(!empty($error)) {
$true = true;
$res = array('info' => '$error', 'error' => '$true');
echo json_encode($res);
die();
} else {
// no error
echo "<div> no error </div>";
}
I think my problem lies in the dataType:json parameter because it expects JSON_ENCODED format? I just want to append the <DIV> (non json_encoded) if the else condition is met.
$.ajax
({
type : 'POST',
url : 'submitForm.php',
data : formData,
dataType : 'json',
success : function(data) {
if(data.error == true) {
console.log(data.info); //display error
} else {
console.log(data);
//some jquery to append the <div>
}
}
})
Checking the headers appears to be okay, and the preview tab, returns the <DIV> data
Request Method:POST
Status Code:200 OK
But its not appending, Nor the <DIV> being shown in console.log.
Is there a way i could disable the dataType if a certain PHP condition is met? OR, a proper way of handling json_encoded along side with non json_encoded format in the same PHP file?
dataType : 'json',is optional, you don't have to explicitly define the data type. Just remove that option.