如何将JS数组传递给PHP数组 [英] How to pass a JS array to PHP array

查看:387
本文介绍了如何将JS数组传递给PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将JS数组转换为PHP数组。我试图将值发送到PHP页面:



  var  seatsReserved = []; 

function reserve(seat){
seat.setAttribute( class reserved) ;
var hasClass = seat.classList.contains(' 保留');
if (hasClass){
seatsReserved.push( parseInt (seat.innerHTML) ));

$ .post(' ../ index.php',{ ' seatsReserved' JSON .stringify( seatReserved)});
console .log(seatsReserved.sort( function (a,b){返回 ab}));
}
}





然而,当我跑步时



  $ seatsReserved  = json_decode($ _ POST ['' < span class =code-string> seatsReserved']); 



我收到此错误:



注意:未定义索引:seatsReserved 





如何正确地将数组传递给PHP ?

解决方案

.post(' ../ index .php',{' seatsReserved' JSON .stringify(seatsReserved)});
console .log(seatsReserved.sort( function (a,b){返回 ab}));
}
}





然而,当我跑步时



  


seatsReserved = json_decode(


< blockquote> _POST [' seatsReserved']);



我收到此错误:



注意:未定义索引:seatsReserved 





如何才能正确地将数组传递给PHP?


I would like to convert a JS array to a PHP array. I tried to send the value to the PHP page:

var seatsReserved = [];

function reserve(seat){
seat.setAttribute("class","reserved");
var hasClass = seat.classList.contains('reserved');
if (hasClass) {
seatsReserved.push(parseInt(seat.innerHTML));

$.post('../index.php', { 'seatsReserved': JSON.stringify(seatsReserved) });
console.log(seatsReserved.sort(function(a, b){return a-b}));
}
}



However, when I run

$seatsReserved = json_decode($_POST['seatsReserved']);


I get this error:

Notice: Undefined index: seatsReserved



How can I correctly pass the array to PHP?

解决方案

.post('../index.php', { 'seatsReserved': JSON.stringify(seatsReserved) }); console.log(seatsReserved.sort(function(a, b){return a-b})); } }



However, when I run


seatsReserved = json_decode(


_POST['seatsReserved']);


I get this error:

Notice: Undefined index: seatsReserved



How can I correctly pass the array to PHP?


这篇关于如何将JS数组传递给PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆