I saw many examples related but I'm still confused. I'm using ajax (which I don't know much about) to get the results of a file updated every xxx seconds. It's working perfectly if I pass just one variable, but what is the best way if I need to pass an array from php through it?

Structure is simples:

show_results.php

<?php
include_once('modWhosonlineCustom.inc.php');
$document->addScript("http://code.jquery.com/jquery-latest.js");
$document->addScript("ajax.js");
$array_name = modWhosonlineCustom::getOnlineUserNames();//the array I need to pass to javascript variable
?>

<script>
var whosonline = '<?php echo "$array_name"; ?>';
</script>

<div id="results"></div>

Ajax code than would have more than one param to build in the url load:

  ajax.js

  $(document).ready(function() {
    $("#results").load("response.php?array_name[param1]&array_name[param2]");
  var refreshId = setInterval(function() {
  $("#results").load("response.php?array_name[param1]&array_name[param2]&randval="+ Math.random());
  }, 10000);
  $.ajaxSetup({ cache: false });
  });

And back to PHP response page, how could I use again the array params passed through url?

  response.php

  <?php
  $names = $_GET['array_name'];
  foreach ($names as $name) {
  //do something

Any suggestions is really appreciated, thanks!

EDIT

Thanks guys, I think I'm the right way now, but ramains the problem to pass this array through a url in the javascript. Or maybe I'm not getting it in the right way in the php end callback file. I'll show you what a modifyed:

   show_results.php

   ...
   <?php
   $names = modWhosonlineCustom::getOnlineUserNames();
   ?>

   <script>
   var whosonline = '<?php echo "json_encode($names)"; ?>';
   </script>


   ajax.js


   $(document).ready(function() {
   $("#atendentes").load("response.php?names=" + whosonline);
   var refreshId = setInterval(function() {
   $("#atendentes").load("response.php?names=" + whosonline + "&randval="+ Math.random());
   }, 10000);
   $.ajaxSetup({ cache: false });
   });


   response.php


   $users = $_GET['names'];
   $users = json_decode($users);
   echo "user: $users";

   $names = $users;
   foreach ($names as $name) {
   ...

Here in the other side I'm getting: Warning: Invalid argument supplied for foreach() in response.php on line 33, and the echo is empty

What is missing?

share|improve this question

73% accept rate
feedback

2 Answers

To pass arrays/objects from PHP to JavaScript you may use the json_encode()/json_decode() functions.

share|improve this answer
Thank you! I went through the manual and modifyed the script, but ramain the problem now on how to get the array again from the url generated by javascript. Please see the edited question, thank you! – Adry Jan 21 '12 at 15:46
feedback

Your code wouldn't work. If you have

$arr = array('a', 'b', 'c');
echo $arr;

you actually get

Array

as the output. Not the contents of the array. To "output" an array from PHP to JS, you have to convert it to native Javascript, which is where json_encode comes in:

<?php
$arr = array('a', 'b', 'c');
?>
var js_array = <?php echo json_encode($arr) ?>;

which will produce

var js_array = ["a","b","c"];

As a general rule, anytime you are using PHP to generate javascript code and are filling in Javascript variables with PHP values, you should use json_encode to ensure that you're generating valid Javascript. Any syntax errors and the whole Javascript code block is dead in the water once the client starts trying to execute it.

share|improve this answer
Thanks a lot Marc B. I think my problem now is how to pass get the array back in the php callback file in a usable way. I'm editing my question with your suggestion to give you an idea, if you can give me another clue. Thanks! – Adry Jan 21 '12 at 15:45
There's JSON.stringify to do the JS-side equivalent of json_encode(), which could then url-encode and put into a query string... but consider using post to transfer this, as long query strings get truncated by browsers and servers. – Marc B Jan 21 '12 at 17:03
Hi Mark B, how can I use POST to approuch this since the url is used to call the file to be updated automaticly? I don't know how to build a logic with this approuch, could you give me an example? – Adry Jan 22 '12 at 10:33
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.