如何在我的HTML表单中使用发布到PHP的数组格式输入字段名称? [英] How can I use the array-format input field names in my HTML form that posts to PHP?

查看:146
本文介绍了如何在我的HTML表单中使用发布到PHP的数组格式输入字段名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解使用数组格式的HTML输入名称的基础知识。如果我有一个可变数量的'item'输入的表单,我可能会为每个输入做这样的事情:

 < ; input name ='item []'type ='text'/> 

当我从$ _POST数组中检索项时,我可以像这样迭代它们: p>

  $ items = $ _POST ['item']; 
foreach($ items为$ item){
}

但我的问题稍微复杂一些。我有一个表单,用户可以点击一个按钮添加一个,一个新的行将出现在表单的底部。每一个新行都包含一个'name'和'description'输入。

所以最初我以为我会这样做:

 < input name ='item [name] []'type ='text'/> 
< input name ='item [description] []'type ='text'/>

然后像这样遍历它们:

  $ items = $ _POST ['item']; 
foreach($ items为$ item){
print $ item ['name']。 ''。 $项[描述];
}

但不是按照我所希望的那样工作,而是将item数组这样我就可以使用 $ item ['name'] [0] 而不是 $ item [0] ['name ']



然后我翻转它,以便我的输入被命名为:

 < input name ='item [] [name]'type ='text'/> 
< input name ='item [] [description]'type ='text'/>

但是这导致每个名称和每个描述都有一个单独的项目而不是将每一对分组在单个项目中。



我真的不喜欢有'name'数组和'description'数组。我宁愿使用包含'name'和'description'字段的每个数组的'item'数组。有没有什么办法可以做到这一点,而不会在我的JavaScript中生成索引?由于人们可以动态地添加和删除它们,所以我的javascript很难为下一个项目计算适当的索引。有没有办法做到这一般?

解决方案

这是不可能做你想做的,但如果它有帮助,这里有一些( item_name [] 和 item_description [] )的代码: p>

  $ items_desc = $ _POST [item_description]; 
$ items_name = $ _POST [item_name];
$ items = array();
for $($ i = 0; $ i< count($ item_name); $ i ++)
{
$ items [] = array(name=> $ item_name [$ i],description=> $ items_desc [$ i]);
}


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:

<input name='item[]' type='text' />

And when I retrieve the items from the $_POST array I could iterate over them like so:

$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'].

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?

解决方案

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[]):

$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]);
}

这篇关于如何在我的HTML表单中使用发布到PHP的数组格式输入字段名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆