0

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?

0

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't echo an array, but you can print_r it for example in the php file being called.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.