HTML 表单:POST 对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34313657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML Form: POST an array of objects
提问by csi
Submitting a class roster. Adding 3 students at once. Each student has first, last, age.
提交班级名册。一次增加3个学生。每个学生都有第一个、最后一个年龄。
Question:How can we get all of the students in an array of arrays?
问题:我们怎样才能得到一个数组中的所有学生?
students[0] => Array (
["first"] => "first name for 0",
["last"] => "last name for 0",
["age"] => "age for 0"
),
students[1] => Array (
["first"] => "first name for 1",
["last"] => "last name for 1",
["age"] => "age for 1"
),
...
Details
For one student:
一名学生的详细信息:
<input type="text" name="first">
<input type="text" name="last">
<input type="text" name="age">
We can return multiple students in separate arrays like this:
我们可以像这样在单独的数组中返回多个学生:
<input type="text" name="students[first][]">
<input type="text" name="students[last][]">
<input type="text" name="students[age][]">
which returns an array of firsts, lasts and ages
它返回一个第一个、最后一个和年龄的数组
students["first"] = [array of first names]
students["last"] = [array of last names]
students["age"] = [array of ages]
Theoretically we can get all the info for a student by accessing the same index (say "3" for each array).
从理论上讲,我们可以通过访问相同的索引(例如每个数组的“3”)来获取学生的所有信息。
We do not want to programatically add an index in the form.
Do not want:
我们不想以编程方式在表单中添加索引。
不要:
<input type="text" name="students[hardcoded_index][first]">
<input type="text" name="students[hardcoded_index][last]">
<input type="text" name="students[hardcoded_index][age]">
If for any reason it matters, we are using Rails for views but can use form helpers or HTML.
如果出于任何原因,我们使用 Rails 来处理视图,但可以使用表单助手或 HTML。
回答by messanjah
tl;dr: Add empty brackets ([]
) after students
to the input names.
tl;dr:在输入名称[]
之后添加空括号 ( ) students
。
Fiddling with Rack::Utils.parse_nested_queryit seems you can get the payload you want like this:
摆弄Rack::Utils.parse_nested_query似乎你可以像这样获得你想要的有效载荷:
<!-- first student -->
<input type="text" name="students[][first]">
<input type="text" name="students[][last]">
<input type="text" name="students[][age]">
<!-- second student -->
<input type="text" name="students[][first]">
<input type="text" name="students[][last]">
<input type="text" name="students[][age]">
Note the empty brackets ([]
) after students
.This tells Rack you want the students
param to be an array. Subsequent params encountered (with the same name) will start a new element.
注意[]
后面的空括号 ( ) students
。这告诉 Rack 你希望students
参数是一个数组。遇到的后续参数(具有相同名称)将开始一个新元素。
POST /myroute?students[][first]=foo&students[][last]=bar&students[][age]=21&students[][first]=baz&students[][last]=qux&students[][age]=19
POST /myroute?students[][first]=foo&students[][last]=bar&students[][age]=21&students[][first]=baz&students[][last]=qux&students[][age]=19
Gets parsed like this:
解析如下:
{"students" => [
{
"first" => "foo",
"last" => "bar",
"age" => "21"
},
{
"first" => "baz",
"last" => "qux",
"age" => "19"
}
]}
Further reading: http://codefol.io/posts/How-Does-Rack-Parse-Query-Params-With-parse-nested-query
进一步阅读:http: //codefol.io/posts/How-Does-Rack-Parse-Query-Params-With-parse-nested-query
回答by Fahad
I know the question is old , but I would like to add my experiences also for future readers.
我知道这个问题很老,但我也想为未来的读者添加我的经验。
For those of you who want to process the datain a PHP enviroment , @messanjah's method won't work ,
对于那些想在 PHP 环境中处理数据的人来说,@messanjah 的方法不起作用,
The methods for parsing data like the built-in serializeArray
or serialize
are not parsing it as expected either.
解析数据的方法(如内置的)serializeArray
或serialize
未按预期解析数据。
This is what I tried so far ...
这是我迄今为止尝试过的...
students[name]
students[][name]
- Very Strange since it was meant to automatically index the arraystudents[name][]
students[name]
students[][name]
-很奇怪,因为它是为了自动索引数组students[name][]
Neither of them worked, but this students[<hardcoded-index>][name]
worked for PHP,
他们都没有工作,但这students[<hardcoded-index>][name]
对PHP 有效,
I know that although the question is against this method, but it will be useful for PHP users who will land here in the nearby future as the asker needed it for Ruby On Rails.
我知道虽然问题是反对这种方法的,但它对于在不久的将来登陆这里的 PHP 用户很有用,因为 Ruby On Rails 需要它。
The methodyou choose to hard code the indexes is upto you , you can use a cleaner method by using javascriptor you can manually hard code them initially in your form element.
该方法你选择硬编码索引是高达你,你可以通过使用清洁方法的JavaScript或者您也可以手动硬鳕ê他们最初是在你的表单元素。
Cheers
干杯