简体   繁体   English

如何在PHP表单处理器中获取多组复选框值?

[英]How to I get multiple groups of checkbox values in php form processor?

Looking everywhere, but can't find solution. 环顾四周,却找不到解决方案。 Everyone talks about one group like so: 每个人都这样谈论一组:

<input type="checkbox" value="red" name="colors[]"> red<br />
<input type="checkbox" value="purple" name="colors[]"> purple<br>
<input type="checkbox" value="blue" name="colors[]"> blue<br>
<input type="checkbox" value="black" name="colors[]"> black<br>

but i am needing to do multiple groups in a single form like this: 但我需要像这样的单一形式做多个小组:

<input type="checkbox" value="red" name="colors[]"> red<br />
<input type="checkbox" value="purple" name="colors[]"> purple<br>
<input type="checkbox" value="blue" name="colors[]"> blue<br>
<input type="checkbox" value="black" name="colors[]"> black<br>
<input type="checkbox" value="sm" name="sizes[]"> small<br />
<input type="checkbox" value="med" name="sizes[]"> medium<br>
<input type="checkbox" value="lrg" name="sizes[]"> large<br>
<input type="checkbox" value="xlrg" name="sizes[]"> x-large<br>

and on top of that the form is dynamic. 最重要的是,表格是动态的。 the names are variable and unknown, so in php post code, it can't be $_POST['colors']. 名称是可变的并且是未知的,因此在php邮政编码中,它不能是$ _POST ['colors']。

i have this snippet which can grab all unknown names and build a message for later inserting into an email script for emailing the values of submitted form: 我有这个代码段,可以抓取所有未知的名称,并生成一条消息,供以后插入到电子邮件脚本中,以便通过电子邮件发送提交的表单的值:

foreach ($_POST as $field=>$value) {
    if ($field != "submit") $msg .= $field . ": " . $value . "\n";
}

but as you probably know, when it gets to a set of checkboxes, it says the value is "array", so not only does it need to split or implode the array into multiple values for checkboxes, it then needs to do that for multiple groups of checkboxes. 但是您可能知道,当涉及到一组复选框时,它表示该值为“数组”,因此,不仅需要将数组拆分或内含为复选框的多个值,然后还需要对多个复选框执行此操作复选框组。

so for example this might be what $msg would be on a particular form: 因此,例如,这可能是$ msg在特定表单上的形式:

first_name: first
last_name: last
email_address: email@email.com
phone: 1234567890
variable_radio_name: answer
variable_selectbox_name: answer
colors_from_checkbox_group_one: red,blue
sizes_from_checkbox_group_two: med,lrg
variable_textarea_name: blah blah blah

textboxes, textareas, radios, dropdown boxes are all easy because it's one answer a piece, but those checkboxes are a pain. 文本框,文本区域,收音机,下拉框都很容易,因为它是一个答案,但是这些复选框很麻烦。

EDIT 编辑

did it like this: 是这样的:

if ($field != "submit") $msg .= $field . ": " . is_array($value) ? implode(',', $value) . "\n" ? $value . "\n";

and like this: 像这样:

if ($field != "submit") {
    $msg .= $field . ": " . is_array($value) ? implode(',', $value) . "\n" ? $value . "\n";
}

syntax error both ways. 两种语法都出错。

Your syntax error is in using ? 您的语法错误是在使用? twice instead of : for the send part of the ternary. 两次,而不是:用于三元组的发送部分。 Also, you need parentheses to make the concatenation work correctly: 此外,还需要括号以使串联正常工作:

$msg .= $field . " : " . (is_array($value) ? implode(',', $value) . "\n" : $value . "\n");

This might be more readable: 这可能更具可读性:

if ($field != "submit") {
    if(is_array($value)) {
        $msg[] = "$field : " . implode(',', $value);
    } else {
        $msg[] = "$field : $value";
    }
}
$msg = implode("\n", $msg);

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

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