I am trying to pass back an array with named indexes (e. g.: data.sugg_id) for a post request. This is how my javascript / jQuery looks like:
$.post('submit_text.php', JSON.stringify({'unit_id' : unit_id,
'text' : text,
'ignore_warnings' : ignore_warnings
}),
function(data) {
sugg_id = data.sugg_id;
if (data.status == 'failure') {
warning_history = {}
showMessage(errorBox, data.message);
warnBox.fadeOut("fast");
}
My submit_text.php looks like:
<?
header("Content-Type: application/json");
$unit_id = $_POST["unit_id"];
$text = $_POST["text"];
$ignore_warnings = $_POST["ignore_warnings"];
$sugg_id = 24;
//$status = "success";
$status = $text;
$message = "";
$data = array("sugg_id" => $sugg_id, "status" => $status, "message" => $message);
echo json_encode($data);
?>
I tried to use alert(data.status) but it said "undefined". What am I doing wrong?