简体   繁体   English

如何遍历动态表单输入并插入数组

[英]How to loop through dynamic form inputs and insert into an array

I have a form that asks users to input numbers into multiple form fields. 我有一个表格,要求用户在多个表格字段中输入数字。 They can select how many form fields they would like. 他们可以选择想要的表单字段数。 I have labelled the form fields as df1, df2, df3 etc. For each extra form field the user adds it will simply plus 1 to the number after df. 我已将表单字段标记为df1,df2,df3等。对于每个额外的表单字段,用户将其添加到df之后的数字将简单地加上1。 My problem it trying to capture (using PHP) these fields and put them in an array. 我的问题是尝试捕获(使用PHP)这些字段并将它们放入数组中。 To complicate matters the user might not complete a field, eg df2 or df15. 为了使事情复杂化,用户可能未填写字段,例如df2或df15。 I am looking for a way to sort through the post form fields that start df1 but eliminate any fields that have been left blank. 我正在寻找一种方法来对以df1开头的帖子表单字段进行排序,但要消除所有留空的字段。 Then, insert their values (numbers) into an array. 然后,将其值(数字)插入数组。 Unfortunately, I am getting confused as to how to screen out the posted form fields that are NULL. 不幸的是,我对如何筛选出张贴为NULL的表单字段感到困惑。 I have been trying to loop through the fields and increment the number after df but without success. 我一直试图遍历这些字段并在df之后增加数字,但没有成功。

Any help would be very much appreciated. 任何帮助将不胜感激。

Many thanks, 非常感谢,

Adam 亚当

Note that you can also give your form fields indexed names: 请注意,您还可以为表单字段添加索引名称:

<input type="text" name="df[1]" />
<input type="text" name="df[2]" />

They will then be available in an array , for example (depending on your submit method): 然后,它们将以数组形式提供(例如(取决于您的Submit方法)):

foreach ($_GET['df'] as $num => $val) {
    …
}

If the index is left out of the form names, PHP will index them automatically, just as when assigning to an array . 如果索引不包含在表单名称中,PHP将自动为它们建立索引,就像分配给array一样

<input name="df[]" />
<input name="df[]" />

You can create multidimensional arrays this way: 您可以通过以下方式创建多维数组:

<label>Foo</label><input name="df[foo][]" /> <!-- $_REQUEST['df']['foo'][0] -->
<label>Bar</label><input name="df[bar][]" /> <!-- $_REQUEST['df']['bar'][0] -->

<label>Foo</label><input name="df[foo][]" /> <!-- $_REQUEST['df']['foo'][1] -->
<label>Bar</label><input name="df[bar][]" /> <!-- $_REQUEST['df']['bar'][1] -->

Note that each [] will cause the generated index to be incremented. 请注意,每个[]都会使生成的索引递增。 This means it isn't terribly useful for multidimensional arrays, other than for the last index. 这意味着除了最后一个索引,它对于多维数组并不是非常有用。 For example, suppose you wanted each $_REQUEST['df'] to hold an array('foo'=>..., 'bar'=>...) . 例如,假设您希望每个$_REQUEST['df']都持有一个array('foo'=>..., 'bar'=>...) The following wouldn't work: 以下内容不起作用:

<label>Foo</label><input name="df[][foo]" /> <!-- $_REQUEST['df'][0]['foo'] -->
<label>Bar</label><input name="df[][bar]" /> <!-- $_REQUEST['df'][1]['bar'] -->

Instead, you'd need: 相反,您需要:

<label>Foo</label><input name="df[0][foo]" />
<label>Bar</label><input name="df[0][bar]" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM