I saw many good examples related but I am still confused. I am using ajax (which I'm not sure much about) to obtain the outcomes of personal files up-to-date every xxx seconds. It's working perfectly basically pass only one variable, but what's the easiest way if I have to pass an assortment 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 might have several param to construct within 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 to PHP response page, how could I personally use again the array params passed through url?

  response.php

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

Any suggestions is actually appreciated, thanks!

EDIT

Thanks men, I believe I am the proper way now, but ramains the issue to pass through this array via a url within the javascript. Or possibly I am not setting it up within the right means by the php finish callback file. I'll demonstrate exactly 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) {
   ...

Within sleep issues I am getting: Warning: Invalid argument provided for foreach() in reaction.perl online 33, and also the echo is empty

What's missing?

To pass through arrays/objects from PHP to JavaScript you can utilize the [cde]/[cde] functions.

Your code wouldn't work. For those who have

json_encode()

you really get

json_decode()

because the output. Not the items in the array. To "output" an assortment from PHP to JS, you need to convert it to native Javascript, that is where json_scribe is available in:

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

that will produce

Array

Typically, as you are using PHP to create javascript code and therefore are filling out Javascript variables with PHP values, you need to use json_scribe to make sure that you are producing valid Javascript. Any syntax errors and also the whole Javascript code block is dead within the water when the client begins attempting to carry it out.