I've been using Serialize() to pass checkbox form data with Post() for a basket that can hold multiple items of the same category.
我一直在使用Serialize()来传递带有Post()的复选框表单数据,用于可以容纳同一类别的多个项目的篮子。
When I post them using the submit button it works fine with multiple values being passed and displayed under one category.
当我使用提交按钮发布它时,它可以正常工作,并传递多个值并显示在一个类别下。
However when I used Jquery serialize() it will only show one item per category and only two categories in total. This is an array issue but I cannot work it out.
但是当我使用Jquery serialize()时,它每个类别只显示一个项目,总共只显示两个类别。这是一个数组问题,但我无法解决。
Is there an alternative JQuery function i should be using to pass a multi-dimensional array?
是否有一个替代JQuery函数我应该用于传递一个多维数组?
5 个解决方案
#1
13
Jquery will take multi dimensional arrays directly, no need to serialize.
Jquery将直接采用多维数组,无需序列化。
var data = {
foo: 123,
bar: 456,
rows: [
{
column1 : 'hello',
column2 : 'hola',
column3 : 'bonjour',.
},
{
column1 : 'goodbye',
column2 : 'hasta luego',
column3 : 'au revoir',
},
],
test1:{
test2: {
test3: 'baz'
}
}
};
_Post Data in your PHP file would look like this
PHP文件中的_Post数据如下所示
Array
(
[foo] => 123
[bar] => 456
[rows] => Array
(
[0] => Array
(
[column1] => hello
[column2] => hola
[column3] => bonjour
)
[1] => Array
(
[column1] => goodbye
[column2] => hasta luego
[column3] => au revoir
)
)
[test1] => Array
(
[test2] => Array
(
[test3] => baz
)
)
)
Once you define your data multidimensional array, your Ajax could be as simple as
一旦定义了数据多维数组,您的Ajax就可以如此简单
$.ajax({
type: 'post',
cache: false,
url: './ajax.php',
data: data
});
If your post array may have fields that you don't know about, you can access your Post array in your php file easily with
如果你的帖子数组可能包含你不知道的字段,你可以轻松地在php文件中访问你的Post数组
$data = file_get_contents('php://input');
$data = json_decode($data, true);
#2
2
I did not find any good solution, so i solved this using JSON.stringify(); here is my code
我没有找到任何好的解决方案,所以我使用JSON.stringify()解决了这个问题。这是我的代码
Client side :
客户端 :
var data = {a:{'foo':'bar'},b:{'this':'that'}};
$.ajax({ url : '/',
type : 'POST',
data : {'data':JSON.stringify(data)},
success : function(){ }
});
Server Side:
服务器端:
$data = json_decode($_POST['data']);
print_r($data);
// Result:
// Array( "a" => Array("foo"=> "bar"), "b" => Array("that" => "this"))
#3
1
$.post(url, {"myarray":arrayData}, function(data){/*stuff*/}, 'json');
server side you would access it for example with php
服务器端你可以访问它,例如用PHP
$myArray = $_POST['myarray'][0];
foreach($myArray as $item)
{
/*logic here for picking apart your array*/
}
#4
0
From the jQuery docs:
来自jQuery文档:
For a form element's value to be included in the serialized string, the element must have a name attribute. Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked.
要使表单元素的值包含在序列化字符串中,该元素必须具有name属性。复选框和单选按钮(“radio”或“checkbox”类型的输入)中的值仅在选中时才包括在内。
Check your code for that first. Hard to help further without seeing your code.
首先检查您的代码。如果没有看到您的代码,很难提供帮助。
#5
0
Here is my metacode fragment, which works fine for me ...
这是我的metacode片段,对我来说效果很好......
var f={};
f.text = "SOME DATA";
f.any_other_field = some_other_value;
f.items = [];
$("#droppable .or").each(function(ee){
var tmp={};
tmp.id = $(this).data("cid");
tmp.name = $(this).find(".ornazev").text();
tmp.price = $(this).data("price");
tmp.currency = $(this).data("currency");
tmp.ks = 1;
f.items.push(tmp);
});
$.ajax({
type: "POST",
url: urlsave,
data: {"f":f},
dataType: "text",
.....
#1
13
Jquery will take multi dimensional arrays directly, no need to serialize.
Jquery将直接采用多维数组,无需序列化。
var data = {
foo: 123,
bar: 456,
rows: [
{
column1 : 'hello',
column2 : 'hola',
column3 : 'bonjour',.
},
{
column1 : 'goodbye',
column2 : 'hasta luego',
column3 : 'au revoir',
},
],
test1:{
test2: {
test3: 'baz'
}
}
};
_Post Data in your PHP file would look like this
PHP文件中的_Post数据如下所示
Array
(
[foo] => 123
[bar] => 456
[rows] => Array
(
[0] => Array
(
[column1] => hello
[column2] => hola
[column3] => bonjour
)
[1] => Array
(
[column1] => goodbye
[column2] => hasta luego
[column3] => au revoir
)
)
[test1] => Array
(
[test2] => Array
(
[test3] => baz
)
)
)
Once you define your data multidimensional array, your Ajax could be as simple as
一旦定义了数据多维数组,您的Ajax就可以如此简单
$.ajax({
type: 'post',
cache: false,
url: './ajax.php',
data: data
});
If your post array may have fields that you don't know about, you can access your Post array in your php file easily with
如果你的帖子数组可能包含你不知道的字段,你可以轻松地在php文件中访问你的Post数组
$data = file_get_contents('php://input');
$data = json_decode($data, true);
#2
2
I did not find any good solution, so i solved this using JSON.stringify(); here is my code
我没有找到任何好的解决方案,所以我使用JSON.stringify()解决了这个问题。这是我的代码
Client side :
客户端 :
var data = {a:{'foo':'bar'},b:{'this':'that'}};
$.ajax({ url : '/',
type : 'POST',
data : {'data':JSON.stringify(data)},
success : function(){ }
});
Server Side:
服务器端:
$data = json_decode($_POST['data']);
print_r($data);
// Result:
// Array( "a" => Array("foo"=> "bar"), "b" => Array("that" => "this"))
#3
1
$.post(url, {"myarray":arrayData}, function(data){/*stuff*/}, 'json');
server side you would access it for example with php
服务器端你可以访问它,例如用PHP
$myArray = $_POST['myarray'][0];
foreach($myArray as $item)
{
/*logic here for picking apart your array*/
}
#4
0
From the jQuery docs:
来自jQuery文档:
For a form element's value to be included in the serialized string, the element must have a name attribute. Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked.
要使表单元素的值包含在序列化字符串中,该元素必须具有name属性。复选框和单选按钮(“radio”或“checkbox”类型的输入)中的值仅在选中时才包括在内。
Check your code for that first. Hard to help further without seeing your code.
首先检查您的代码。如果没有看到您的代码,很难提供帮助。
#5
0
Here is my metacode fragment, which works fine for me ...
这是我的metacode片段,对我来说效果很好......
var f={};
f.text = "SOME DATA";
f.any_other_field = some_other_value;
f.items = [];
$("#droppable .or").each(function(ee){
var tmp={};
tmp.id = $(this).data("cid");
tmp.name = $(this).find(".ornazev").text();
tmp.price = $(this).data("price");
tmp.currency = $(this).data("currency");
tmp.ks = 1;
f.items.push(tmp);
});
$.ajax({
type: "POST",
url: urlsave,
data: {"f":f},
dataType: "text",
.....