views:

93

answers:

2

Hello,

I'm trying to use jQuery's $getJSON to send an array of 'ids'. Here's what my jQuery looks like:

var calendarIds = [];
 $("#jammer :selected").each(function(i, selected){
 calendarIds[i] = $(selected).val();
 });

    $.getJSON("test.php", {
      start: start.getTime() / 1000,
      end: end.getTime() / 1000,
      calendarid: calendarIds
          },  
   function(a) {
      callback(a);
    });

And, when I inspect with Firebug, it shows that multiple values are being passed: e.g.,

  http://mydomain.com/test.php?calendarid=3&calendarid=4

However, when I try to join this so-called array using:

$comma_separated = join(",", $_GET['calendarid']);

echo $comma_separated;

I'm getting:

  Warning: join() [function.join]: Invalid arguments passed

And, if I just echo $_GET['calendarid'], I'm getting, it only echos the last id passed, e.g.:

 echo $_GET['calendarid'];   //echos "4"

Any ideas on what I'm doing wrong? Thanks!

+3  A: 

The URL should have been

http://mydomain.com/test.php?calendarid[]=3&calendarid[]=4

In order for PHP to parse $_GET['calendarid'] as a PHP Array.

Once you set it to the correct URL, you will be able to use join() in PHP correctly.

thephpdeveloper
Thanks for the reply: So, using the $.getJSON with jQuery, how can I tell it to send an arary? I tried doing this:$.getJSON("test.php", { start: start.getTime() / 1000, end: end.getTime() / 1000, calendarid[]: calendarIds }, However, having the brackets ([]) after calendarid causes a javascript error. Any ideas on that?
Dodinas
+3  A: 

In reply to your comment to Mauris's answers:
use this in your object of parameters you want to send:

"calendarid[]": calendarIds //

instead of:

 calendarid = calendarIds
Pim Jager
+1 for the assist. Thanks. Didn't realize I needed the quotes. :-/
Dodinas
the quotes are required just to put the "[]". normally if you don't put the quotes, it's fine with Javascript. but in this case you need the "[]", and thus the quotes.
thephpdeveloper