PHP将多个表单字段处理为复选框字段

时间:2014-05-07 04:25:13

标签: php arrays checkbox field

我正在尝试使用API​​参考'custom11746175'将数据处理到字段。但是,此字段具有复选框,我希望在必要时处理多个值。现在,我只有这个,它只会处理一个值“Off-g”,“On-g”,“On-h”或“On-i”(最后定义的)一个):

if (($_POST['custom13346240'] == 'Off-g')) {
$contactData['custom11746175'] = "Off-g";
}
if (($_POST['custom13346240'] != 'Off-g')) {
$contactData['custom11746175'] = "On-g";
}
if ($_POST['custom13500281']) {
$contactData['custom11746175'] = "On-h";
}           
if ($_POST['custom11746175'] == 'Yes') {
$contactData['custom11746175'] = "On-i";
}

如果我想处理所有定义的值(数量可能会有所不同)并在复选框中标记,我需要更改哪些内容?我应该构造一个数组,以获得类似多维场的东西吗?

2 个答案:

答案 0 :(得分:0)

是的,你需要一个数组,如果你在$contactData['custom11746175']变量之后放置双括号[]将新项目添加到$contactData['custom11746175']这样的数组中......

if (($_POST['custom13346240'] == 'Off-g')) {
    $contactData['custom11746175'][] = "Off-g";
}
if (($_POST['custom13346240'] != 'Off-g')) {
    $contactData['custom11746175'][] = "On-g";
}
if ($_POST['custom13500281']) {
    $contactData['custom11746175'][] = "On-h";
}           
if ($_POST['custom11746175'] == 'Yes') {
    $contactData['custom11746175'][] = "On-i";
}

然后,要获得数组中的第一个元素,您只需执行$contactData['custom11746175'][0]

答案 1 :(得分:0)

来自Solve360的团队指出,这些字段允许多个复选框的逗号分隔值。因此我将上面的代码更改为:

$items = "";

if (($_POST['custom13346240'] == 'Off-g')) {
    $items = $items . ',' . "Off-g";
}
if (($_POST['custom13346240'] != 'Off-g')) {
    $items = $items . ',' . "On-g";
}
if ($_POST['custom13500281']) {
    $items = $items . ',' . "On-h";
}            
if ($_POST['custom11746175'] == 'Yes') {
    $items = $items . ',' . "On-i";
}

$contactData['custom11746175'] = $items;

希望能帮助某人。