Is it possible to get a PHP array created on another page via AJAX? This is what I have so far:
index.php
$.ajax({
url: 'array.php',
type: 'GET',
complete: function(data) {
$('#div').html(data.responseText);
}
});
array.php
$arr = array('red', 'blue', 'green');
echo $arr; //echo it?
As you can see, I’m trying to load $arr into a div on index.php. Is this possible?
You can’t get it as a PHP array unless you are staying in PHP. You can get it as an array, however, using json_encode:
echo json_encode($arr);Which can be parsed in javascript.
Use datatype: json (see http://api.jquery.com/jQuery.ajax) or its shortcut, jQuery.getJSON() to read it on the JS side.