I have an AJAX script updating a database via PHP.
I am then trying to return two variables back to the AJAX success function.
Currently, when alerting the data returned they are showing UNDEFINED.
When I return the JSON without stating the part of the array I require, the array displays in full. But only when I state specifically which value from the array I wish to use, I seem to get undefined on both values.
How should I manage these values returned from the PHP?
The AJAX
success: function(data) {
$('#'+data.toUpdate).html(data.quant);
$('#'+data.toUpdate).addClass('updated_grn');
alert('quant:' + data.quant + '\nid:' + data.toUpdate);
}
The PHP
if ($query) {
echo json_encode(array("toUpdate" => $toUpdate, "quant" => $quant));
}
The Result

When I alert(data) this is returned:

The full php
$itemid = ($_POST['itemid']);
$quant = ($_POST['quant']);
$toUpdate = ($_POST['toUpdate']);
$sql = "UPDATE items_list
SET `stock_level` = '$quant'
WHERE item_id = '$itemid'";
$query = mysql_query($sql);
if ($query) {
echo json_encode(array("toUpdate" => $toUpdate, "quant" => $quant));
}
The full AJAX
$.ajax({
type: 'POST',
url: url,
dataType: 'html',
data: {
itemid: itemid,
quant: quant,
toUpdate: toUpdate
},
beforeSend: function() {
$('#'+id+'_num')
.html("<img src='xxxxxxx.com/home/secure/images/gif/ajax-loader.gif'></img>");
},
success: function(data) {
alert(data);
// $('#'+data.toUpdate).html(data.quant);
// $('#'+data.toUpdate).addClass('updated_grn');
// alert('quant:' + data.quant + '\nid:' + data.toUpdate);
}
});
dataType: 'html'todataType: 'json'