views:

51

answers:

3

Hi all...

I have a javascript array say jsArr[]. I want this array to be passed to a php page through the get method. Something like "nextPage.php?arr=jsArr[]".

There i should be able to access the array like "$arr[] = $_GET[arr]" and perform operations like foreach($arr as $key => $val).

Is it possible...?

Thanks a lot in advance...

A: 

One way to achieve this would be jQuery's serialize()

Raveren
Can't use this plugin... i don't want to submit a whole form... just need to pass this array...
SpikETidE
+2  A: 

you need to change your url to be:

nextPage.php?arr[]=js&arr[]=js2

for example.

var_dump($_GET);

outputs: array(1) { ["arr"]=> array(2) { [0]=> string(2) "js" [1]=> string(3) "js2" } }

SilentGhost
Should i get it like "$_GET[arr]" in the php page...?
SpikETidE
@SpikETidE: Yes.
Andy E
Technically, `$_GET['arr']`. It will work without quotes because PHP stupidly treats unknown constants as unquoted strings, but that should never be relied upon. http://us3.php.net/manual/en/language.constants.syntax.php
Adam Backstrom
This works fine.. Thanks....
SpikETidE
+2  A: 

You can also use JSON (JS parser here)

JS:

  var arr = [1, 4, 9];
  var url = '/page.php?arr=' + JSON.stringify(arr);
  window.location.href = url;

PHP:

$arr = isset($_REQUEST['arr']) ? json_decode($_REQUEST['arr']) : array();
Benoit Vidis