如何将任意大小的Javascript数组传递给PHP
我有一个Javascript数组,有很多可能的大小。我有没有办法将它传递到PHP文件进行计算 我想在html中使用类似表单的东西如何将任意大小的Javascript数组传递给PHP,php,javascript,arrays,web-applications,parameters,Php,Javascript,Arrays,Web Applications,Parameters,我有一个Javascript数组,有很多可能的大小。我有没有办法将它传递到PHP文件进行计算 我想在html中使用类似表单的东西 <form id="barfoo" action="foobar.php" method="post"> <input type="hidden" name="foo"> <input type="hidden" name="bar"> <input type="hidden" name="bam"> &l
<form id="barfoo" action="foobar.php" method="post">
<input type="hidden" name="foo">
<input type="hidden" name="bar">
<input type="hidden" name="bam">
</form>
通常,这些输入是字符串。有没有办法让数组作为输入来传递?比如fooarray=['abc','dbe','3r']
,或者fooarray=['1','c','4','5']
。在生成之前,我不知道它的大小。有没有可能把它放在一个表格和通行证上
非常感谢 使用
JSON.stringify(array)
用JavaScript对数组进行编码,然后使用$arr=JSON\u解码($\u POST['jsondata'])将数组编码为JSON,然后将其插入其中一个隐藏项。这可以在表单onsubmit
事件中完成
<form id="barfoo" action="foobar.php" method="post" onsubmit="populate()">
<input type="hidden" id="foo" name="foo">
</form>
<script>
var fooarray=['1','c','4','5'];
function populate()
{
document.getElementById("foo").value = JSON.stringify(fooarray);
}
</script>
可以将它们作为ajax发送吗?
$fooArray = json_decode($_POST['foo']);
print_r($fooArray);