ISSUE SOLVED BY ME.
OLD POST:
I am actually been working for this in weeks and nothing in the web seems to help. I need to send a data from PHP to ajax through json so that a proper error prompt would occur.
Here is my AJAX splice.
$.ajax({
type: "POST",
url: "insert_account.php",
dataType: "json",
data: {'dataString' : dataString},
success: function(data)
{
if(data.status == "error")
{
alert("ERROR");
}
else if(data.status == "success")
{
alert("SUCCESS");
}
},
and in my PHP it goes like this.
if($count > 0) //this count contains value to check if an account exists
{
$response['status'] = "error";
}
else
{
//QUERY TO INSERT NEW ACCOUNT
$response['status'] = "success";
}
$count = 0;
header("Content-type: application/json");
echo json_encode($response);
This doesn't seem to work and I have no idea why. Also, if it is successful it doesn't insert the data in the database though it works completely fine without ajax. Please help. Thanks in advance.
EDIT: THROUGH FIREBUG I HAVE SEEN THAT,
data.status returns an undefined VALUE because .status is not recognized by the ajax, therefore we truncate .status and use data only to fetch the encoded json_encode($response).
EDITED AJAX:
$.ajax({
type: "POST",
url: "insert_account.php",
dataType: "json",
data: {'dataString' : dataString},
success: function(data)
{
if(data == "error")
{
alert("ERROR");
}
else if(data == "success")
{
alert("SUCCESS");
}
},
Hope I have helped others who experienced the same.