duanao6704 2015-11-02 22:42
浏览 55
已采纳

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

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

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 . "
";
}

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:

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) . "
" ? $value . "
";

and like this:

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

syntax error both ways.

  • 写回答

1条回答 默认 最新

  • doucaishi0077 2015-11-02 22:56
    关注

    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) . "
    " : $value . "
    ");
    

    This might be more readable:

    if ($field != "submit") {
        if(is_array($value)) {
            $msg[] = "$field : " . implode(',', $value);
        } else {
            $msg[] = "$field : $value";
        }
    }
    $msg = implode("
    ", $msg);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号