PHP 后复选框帮助
我无法接收在作为表单一部分的复选框字段中选中的项目列表。
我有一个带有几个复选框的 HTML 表格:
HTML
<input type="checkbox" name="carry[]" value="1" />
<input type="checkbox" name="carry[]" value="2" />
<input type="checkbox" name="carry[]" value="3" />
PHP - 这是我用来将表单发布到电子邮件地址的方法
foreach($_POST as $key => $val) {
$body .= $key . " : " . $val . "\r\n";
,我在电子邮件中得到的值为:“carry:Array” - 不是选定的实际值,如何处理选定的复选框数组。 理想情况
下,我想要:“携带:1; 2; 3"(不带引号)
I"m having trouble receiving a list of items that are checked in a field of checkboxes which are part of a form.
I have an HTML table with a few checkboxes:
HTML
<input type="checkbox" name="carry[]" value="1" />
<input type="checkbox" name="carry[]" value="2" />
<input type="checkbox" name="carry[]" value="3" />
PHP - this is what I'm using to post the form to an email address
foreach($_POST as $key => $val) {
$body .= $key . " : " . $val . "\r\n";
I get the value in my email as: "carry: Array" -- not the actual values that are selected. How do I handle an array of checkboxes selected in a form and post it?
Ideally, I would want: "carry: 1; 2; 3" (without the quotes)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以检查该值是否是数组并以不同的方式处理它:
You can check if the value is an array and handle it differently:
如果你想要字符串
'1; 2; 3'
,您应该将数组中的项目连接在一起:但是,如果数组中的任何项目本身有分号,那么这样做自然会导致不明确的结果。
要迭代 post 数组,允许其任何成员成为数组:
或者,如果您不需要对精确格式进行太多控制并且调试转储就可以了(但您需要看到的不仅仅是无用的字符串
'Array'
:If you want the string
'1; 2; 3'
, you should join together the items in the array:However doing so will naturally cause ambiguous results if any of the items in the array themselves have a semicolon in.
To iterate over the post array allowing any of its members to be arrays:
Or, if you don't need so much control over the exact formatting and a debugging dump is fine (but you need to see more than just the useless string
'Array'
:它打印进位:数组,因为这正是它的本质。您需要对其进行循环(第一个循环中的另一个循环)才能访问其中的值:
这完全未经测试,但希望逻辑是合理的:)
It's printing carry: Array because that's exactly what it is. You need to loop over it (another loop inside the first) to access the values inside:
That's completely untested but hopefully the logic is sound :)