I am trying to handle an array coming back from an ajax call. My current situation is a button gets clicked, and when clicked, it fires an ajax call. Here is a snippet of the PHP after ajax has been called..
function count_total() {
$count = get_count();
if ($count == 0) {
$count = 1;
}
$total = get_total();
$response = array('count' => $count, 'total' => $total);
echo $response;
exit;
}
Ok so now this $response variable is passed back to the JS side and if I alert the variable obviously it will say “Array”. So my question is, how can I work with this array? I am trying to get the key/value out?
Or I can’t pass an array to it like that?
Any ideas?
change your echo to be this.
and then instead of alerting the response, parse it with a json parser library like this.
Then you will have a true javascript object not an array because you’re using a associative array.