如何在发布到 PHP 的 HTML 表单中使用数组格式的输入字段名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4709926/
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
How can I use the array-format input field names in my HTML form that posts to PHP?
提问by fumplr
I understand the basics of using the array-formatted HTML input names. If I had a form with a variable number of 'item' inputs I might do something like this for each of them:
我了解使用数组格式的 HTML 输入名称的基础知识。如果我有一个包含可变数量“项目”输入的表单,我可能会为每个输入做这样的事情:
<input name='item[]' type='text' />
And when I retrieve the items from the $_POST array I could iterate over them like so:
当我从 $_POST 数组中检索项目时,我可以像这样迭代它们:
$items = $_POST['item'];
foreach($items as $item) {
}
But my question is slightly more complicated. I have a form where users can click a button "add one" and a new row will appear at the bottom number of the form. Each new row contains a 'name' and 'description' input.
但我的问题稍微复杂一些。我有一个表单,用户可以在其中单击“添加一个”按钮,然后在表单的底部编号处会出现一个新行。每个新行都包含一个“名称”和“描述”输入。
So initially I thought I would do this:
所以最初我想我会这样做:
<input name='item[name][]' type='text' />
<input name='item[description][]' type='text' />
And then iterate over them like so:
然后像这样迭代它们:
$items = $_POST['item'];
foreach($items as $item) {
print $item['name'] . ' ' . $item['description'];
}
But instead of working as I hoped, it instead structures the 'item' array such that I would access the first item name as $item['name'][0]
rather than as $item[0]['name']
.
但是,它并没有像我希望的那样工作,而是构造了 'item' 数组,这样我就可以访问第一个项目名称 as$item['name'][0]
而不是 as $item[0]['name']
。
So then I flipped it so that my inputs were named as such:
然后我翻转它,以便我的输入被命名为:
<input name='item[][name]' type='text' />
<input name='item[][description]' type='text' />
But this resulted in a separate 'item' for each 'name' and for each 'description' rather than grouping each pair in a single 'item'.
但这导致每个“名称”和每个“描述”都有一个单独的“项目”,而不是将每一对分组为一个“项目”。
I really dislike having arrays of 'name' and separate array of 'description'. I would prefer arrays of 'item' with each array containing a 'name' and a 'description' field. Is there any way to accomplish this without generating the an index in my javascript? Since people can add and remove these dynamically it's very difficult for my javascript to calculate the appropriate index for the next item. Is there no way to do this generically?
我真的不喜欢有“名称”数组和单独的“描述”数组。我更喜欢 'item' 数组,每个数组包含一个 'name' 和一个 'description' 字段。有没有办法在不生成我的 javascript 中的索引的情况下完成此操作?由于人们可以动态添加和删除这些内容,因此我的 javascript 很难计算下一项的适当索引。有没有办法一般地做到这一点?
回答by crimson_penguin
It's not possible to do what you want, but if it helps, here's some code to piece it back together that I think will work (with item_name[]
and item_description[]
):
不可能做你想做的事,但如果它有帮助,这里有一些代码可以将它拼凑起来,我认为可以(使用item_name[]
和item_description[]
):
$items_desc = $_POST["item_description"];
$items_name = $_POST["item_name"];
$items = array();
for ($i = 0; $i < count($items_name); $i++)
{
$items[] = array("name" => $items_name[$i], "description" => $items_desc[$i]);
}