在php中从多达七个复选框中提取多个值

时间:2012-02-15 01:53:04

标签: php checkbox

晚上好,我正在开发一个项目,其中包含一个用户可以选择的七个选项列表。我试图让他们所有人用逗号介于两者之间。这不是从数据库中提取,而是从PHP表单中提取。

<html> 
          <tr><td><font color=black>Select all Constituency Groups that apply:</font></td></tr>
          <tr><td><b><hr size=0 color=silver noshade></b></td></tr>
          <tr><td><input value="1st Year Student" type="checkbox" name="chkCon[]" id="ChkCon1" />&nbsp;1st Year Student</font></td></tr>
          <tr><td><input value="2nd Year Student" type="checkbox" name="chkCon[]" id="ChkCon2" />&nbsp;2nd Year Student</font></td></tr>
          <tr><td><input value="4 Year College Professor" type="checkbox" name="chkCon[]"  id="ChkCon3"/>&nbsp;4 Year College Professor</font></td></tr>
          <tr><td><input value="Adjunct Faculty" type="checkbox" name="chkCon[]" id="ChkCon4" />&nbsp;Adjunct Faculty</font></td></tr>
          <tr><td><input value="Alumni/Graduate" type="checkbox" name="chkCon[]"  id="ChkCon5"/>&nbsp;Alumni/Graduate</font></td></tr>
          <tr><td><input value="High School Teacher" type="checkbox" name="chkCon[]" id="ChkCon6" />&nbsp;High School Teacher</font></td></tr>
          <tr><td><input value="Industry Representative" type="checkbox" name="chkCon[]" id="ChkCon7" />&nbsp;Industry Representative</font></td></tr>
        </table>
</html>

我不确定我是否需要将它们命名为相同或不同。但这里是帖子

if (!empty($_REQUEST['chkCon[]'])) {
    $ChkCon = $_REQUEST['chkCon[]'];
} else {
    $ChkCon = ""; 
}

谢谢

1 个答案:

答案 0 :(得分:0)

$chkCon = $_REQUEST['chkCon'];
$values = "";

for ($i=0;$i<count($chkCon);$i++) { 
    $values .= $chkCon[$i];
    if ($i<count($chkCon)-1) { 
         $values .= ",";
    }
}

echo $values;