Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass a javascript array through GET and access it through $_GET at the other end..?

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...

like image 519
SpikETidE Avatar asked Feb 05 '10 13:02

SpikETidE


2 Answers

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();
like image 119
Benoît Vidis Avatar answered Oct 21 '22 10:10

Benoît Vidis


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" } }

like image 11
SilentGhost Avatar answered Oct 21 '22 10:10

SilentGhost