从多个复选框行收集 $_POST
我有一个包含多行复选框的表单,每个复选框都有一个特定的 id,使用 foreach
循环显示这些复选框。
如何从类似的内容中获取 $_POST
信息?我认为它就像这样 $_POST[][]
,就像一个子数组,但我不知道如何设置它:
foreach($stakholderArray as $currentEntry) {
print "<tr class='$bgcolor'>";
print "<td class='left'>$currentEntry[org]</td>";
if($currentEntry['dataFound']) {
//if data was found for current stakeholder, display it
print ($currentEntry['Partner']) ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox" /></td>';
print ($currentEntry['Agreement']) ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox" /></td>';
print ($currentEntry['Train']) ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox" /></td>';
print ($currentEntry['Meet']) ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox" /></td>';
}
else { //else...no stakeholder data, display empty columns
print "<td><input type ='checkbox'/></td><td><input type ='checkbox'/></td><td><input type ='checkbox'/></td><td><input type ='checkbox'/></td><td><input type ='checkbox'/></td>";
print "</tr>";
}## Heading ##
I have a form with multiple rows of checkboxes, each with a specific id, that are being displayed using a foreach
loop.
How do you grab the $_POST
info from something like that? I think it is like this somehow $_POST[][]
, like a sub-array, but I cant figure out how to set it up:
foreach($stakholderArray as $currentEntry) {
print "<tr class='$bgcolor'>";
print "<td class='left'>$currentEntry[org]</td>";
if($currentEntry['dataFound']) {
//if data was found for current stakeholder, display it
print ($currentEntry['Partner']) ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox" /></td>';
print ($currentEntry['Agreement']) ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox" /></td>';
print ($currentEntry['Train']) ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox" /></td>';
print ($currentEntry['Meet']) ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox" /></td>';
}
else { //else...no stakeholder data, display empty columns
print "<td><input type ='checkbox'/></td><td><input type ='checkbox'/></td><td><input type ='checkbox'/></td><td><input type ='checkbox'/></td><td><input type ='checkbox'/></td>";
print "</tr>";
}## Heading ##
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这与我之前回答过的一个问题有些相关: 从没有 javascript 的 HTML 表单中发布数组
当提交相关内容 项目应该是这样的:
name="item[collection name][collection name][]"
- 请注意与该集合相关的第一个索引(以便于定位),以及该集合中的空索引含义,有一个数组(而不是单个值)。所以对于你的复选框:在请求数组中最终像这样(比如POST):
它们应该像这样结束:
it's somewhat related to a question i answered before: POST an array from an HTML form without javascript
related items should have like this:
name="item[collection name][collection name][]"
- note the first indices pertaining the set (for easy location), and the empty index meaning in that set, there's an array (instead of single value). so for your check boxes:end up like this in the request array (like say POST):
they should end up like:
为每个
checkbox
元素赋予不同的name
标签(您需要添加name="WhatEverYouwant"
),您将能够通过以下方式获取它: :
示例:
并通过以下方式获取:
Give a different
name
tag to everycheckbox
element (you need to addname="WhatEverYouwant"
)and you will be able to get it by:
Example:
and get it by:
假设您已经在
Assuming that you have that in a
<form>
already, you need to give each input an id. Then in the resulting PHP script, use$_POST['whatever_the_name_is']
(you could also use$_REQUEST
or$_GET
depending on your form action).不是答案,但是 yeowch...减少逻辑中的一些重复 HTML:
应该是
Not an answer, but yeowch... reduce some of the duplicate HTML in your logic:
should be
更改:
收件人:
或:
在 PHP 中访问:
或:
这是有效的,因为添加到
name
属性末尾的[]
/[KEYNAME]
在 PHP 中被视为array
项,因此可以循环通过。您也可以通过这种方式嵌套数组,因此如果您想在单个表单上拥有多个利益相关者,请执行以下操作:Change:
To:
Or:
Access in PHP:
Or:
This works because
[]
/[KEYNAME]
added to the end of thename
attribute is seen as anarray
item in PHP, and can therefore be looped through. You can nest arrays this way as well, so if you want to have multiple stake holders on a single form, do something like this:然后您可以像这样从
$_POST
访问它们You can then access them from
$_POST
like so实际上
id
不应该起作用。正如约瑟夫所说,表单元素以它们的名称作为键发送。所以正确的标签应该是:当你发送表单时,你可以得到像
$_POST 这样的数据['some_name']
如果您愿意,可以将它们放入数组
name="somearr[someotherarr[some_name]]]"
那么内容将在$_POST['somearr']['someotherar']['some_name']
希望有帮助。
Actualy
id
shouldn't work. As Joseph said, form elements are sent with their names as keys. So the proper tag should be:<input type="checkbox" name="some_name" ... />
When you send the form, you can get the data like
$_POST['some_name']
If you like you can put them in an array
name="somearr[someotherarr[some_name]]]"
then the content will be available in$_POST['somearr']['someotherar']['some_name']
Hope that helps.