Php foreach循环插入所有复选框(选中和未选中)

Php foreach循环插入所有复选框(选中和未选中),php,html,mysql,loops,foreach,Php,Html,Mysql,Loops,Foreach,我有一个使用php生成html复选框的表单,如下所示 <p><form name="university" action="/university_handler" method="post"> <fieldset> <table class="table"> <thead> <tr> <th><span class="help-block">

我有一个使用php生成html复选框的表单,如下所示

<p><form name="university" action="/university_handler" method="post">
  <fieldset>
    <table class="table">
      <thead>
        <tr>
          <th><span class="help-block">University Department</span></th>                            
        </tr>
      </thead>
      <tbody>  
        <tr> 
          <td><?php 
            $query = mysqli_query($db, "SELECT university_department FROM university WHERE university_id = '$university_id'") 
                or die  ("Could not search!");
            while($row = mysqli_fetch_array($query)){
              $university_department = $row['university_department'];
              $_SESSION['university_department'] = $university_department;
              $universityDepartment = $_SESSION['university_department'];
              echo "<label><input type='checkbox' name='university_department[]' value='{$universityDepartment}'>$universityDepartment</label><br><input type='text' value='' name='professor_name[{$universityDepartment}]' placeholder='Professor-Name'><input type='text' value='' name='class_name[{$universityDepartment}]' placeholder='Class-Name'>";}
          ?></td>
        </tr>
      </tbody>
    </table> 
    <button type="submit" name="Submit"class="btn btn-info">Submit</button>
  </fieldset>
</form></p>

您可以使用isset功能检查复选框是否已选中

if (isset($_REQUEST['checkBoxName']));
   {

    $variable = $_REQUEST['checkBoxName'];
    }
使用此方法,您将只获得具有值的复选框


希望这有帮助

我喜欢@Martin的答案。我只想更改
$\u请求

当使用
$\u REQUEST
时,您的意思是“hey look at a post”或“get”变量并使用其中一个值。那么,如果一个
$\u POST
和一个
$\u GET
变量的名称相同,会发生什么情况呢?一个将被使用,而另一个不被使用

因此,让我们以不同的方式使用相同的代码

$checkBoxName = (isset($_POST['checkBoxName']) ? $_POST['checkBoxName'] : (isset($_GET['checkBoxName']) ? $_GET['checkBoxName'] : ''));
if ($checkBoxName != '') {
    //do stuff here
}
这样一来,如果你不使用谷歌Chromes开发者工具之类的工具就看不到帖子信息,那么最好按照这个顺序来做,这样你就可以在看到之前先检查没有看到的内容

希望这有帮助()

编辑:

根据你给我的信息,我想出了一种只插入你检查过的类的方法。您可以随意进行更改,但这应该是可行的

<?php
$departList =   ($_POST['university_department'] ? $_POST['university_department'] : ($_GET['university_department'] ? $_GET['university_department'] : array()));
$classList =    ($_POST['class_name'] ? $_POST['class_name'] : ($_GET['class_name'] ? $_GET['class_name'] : array()));
$profList = ($_POST['professor_name'] ? $_POST['professor_name'] : ($_GET['professor_name'] ? $_GET['professor_name'] : array()));
if (count($departList) > 0) {
    foreach ($departList as $key => $val) {
        $class =    $classList[$val];
        $professor =    $profList[$val];
        $query_uni = ("INSERT INTO temp_list(departmentName, class_name, professor_name) VALUE ('$val','$class', '$professor')");
        $q_u = mysqli_query($db, $query_uni) or die ('Error posting data');
    }
}

?>

为什么设置三个不同的变量只使用最后一个<代码>$university\u department=$row['university\u department']
$\u课程[“大学系”]=大学系
$universityDepartment=$\u SESSION['university\u department']
只是一个小小的专业提示:当你通过
包含
要求
包含文件时,你应该使用
包含一次()
要求一次()
,这样以后当你包含其他文件时,你就不会因为定义函数两次或导致加载时间更长而被ping。嗨!当我在if语句中打印$checkBoxName时,它就可以工作了。我现在做的是
if($checkBoxName!=''){foreach(str_替换('#','',$_POST['checkBoxName'])为$Name=>id){print_r($checkBoxName);print'
'}
打印所有复选框…..你能给我一个
打印
变量转储
整个
$\u POST
数组吗。这将帮助我在重读你的问题后找出你的循环,我想我理解,但让我澄清一下。你问的是为什么所有的表单信息都是插入,而不是仅插入复选框中的复选框。是否正确?打印$后给出:数组([大学系]=>数组([0]=>chemistry)[class\u name]=>数组([化学]=>Chem101[生物学]=>[生物化学]=>)[教授姓名=>数组([化学]=>Jones博士[生物学]=>[生物化学]=>)[提交]=>)是的,这是正确的,所有的都被插入,而不是那些复选框被选中的
<?php
$departList =   ($_POST['university_department'] ? $_POST['university_department'] : ($_GET['university_department'] ? $_GET['university_department'] : array()));
$classList =    ($_POST['class_name'] ? $_POST['class_name'] : ($_GET['class_name'] ? $_GET['class_name'] : array()));
$profList = ($_POST['professor_name'] ? $_POST['professor_name'] : ($_GET['professor_name'] ? $_GET['professor_name'] : array()));
if (count($departList) > 0) {
    foreach ($departList as $key => $val) {
        $class =    $classList[$val];
        $professor =    $profList[$val];
        $query_uni = ("INSERT INTO temp_list(departmentName, class_name, professor_name) VALUE ('$val','$class', '$professor')");
        $q_u = mysqli_query($db, $query_uni) or die ('Error posting data');
    }
}

?>