开发者

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

开发者 https://www.devze.com 2022-12-19 01:28 出处:网络
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[].

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开发者_Go百科 it possible...?

Thanks a lot in advance...


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();


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消