如果您一直在尝试发送一维数组并且jquery 正在将其转换为逗号分隔值 >:(然后按照下面的代码将实际数组提交给php
而不是所有逗号分隔的公牛**它。
假设您必须附加一个名为myvals
.
jQuery('#someform').on('submit', function (e) {
e.preventDefault();
var data = $(this).serializeArray();
var myvals = [21, 52, 13, 24, 75]; // This array could come from anywhere you choose
for (i = 0; i < myvals.length; i++) {
data.push({
name: "myvals[]", // These blank empty brackets are imp!
value: myvals[i]
});
}
jQuery.ajax({
type: "post",
url: jQuery(this).attr('action'),
dataType: "json",
data: data, // You have to just pass our data variable plain and simple no Rube Goldberg sh*t.
success: function (r) {
...
现在php
当你这样做的时候里面
print_r($_POST);
你会得到 ..
Array
(
[someinputinsidetheform] => 023
[anotherforminput] => 111
[myvals] => Array
(
[0] => 21
[1] => 52
[2] => 13
[3] => 24
[4] => 75
)
)
请原谅我的语言,但是有很多Rube-Goldberg解决方案散布在整个网络上,特别是在 SO 上,但没有一个是优雅的,也没有解决通过 ajax post实际发布一维数组php
的问题。不要忘记传播这个解决方案。