Q:

foreach loop inserting all the checkboxes (checked and unchecked)

I have a form that generates html checkboxes using php which is shown below
<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>

Now when I use my university_handler to insert the values into the database all of the check boxes are inserting instead of just the ones that have been checked off. I've been trying a range of things be nothing seems to be working. Here is the handler.
<?php
session_start();
include("connect.php");

$university_id = $_SESSION['university_id'];

// check if share_form is submitted and not empty
$error_message = "";
if(is_array($_POST['university_department']) && !empty($_POST['university_department'])){
  $error = array();
  $universityDepartment = $_POST['university_department'];
  if (count($universityDepartment)>0){
    foreach (str_replace('#', '', $_POST['class_name']) as $departmentName => $stripid){
      $class_name_backslash = $stripid . '/';
      $class_name = mysqli_real_escape_string($db, $stripid);
      print_r($class_name);
    }
    $query_uni = ("INSERT INTO temp_list(departmentName, class_name, professor_name) VALUE ('$departmentName','$class_name', '$professor_name')");
    $q_u = mysqli_query($db, $query_uni) or die ('Error posting data');
}
}?>

A:

I like @Martin's answer. The only thing i want to change is the $_REQUEST
When using $_REQUEST you are saying hey look at a post or get variable and use either value. So what happens if you have both a $_POST and a $_GET variable with the same name? One will be used while the other does not.
So lets use the same code in a different way.
$checkBoxName = (isset($_POST['checkBoxName']) ? $_POST['checkBoxName'] : (isset($_GET['checkBoxName']) ? $_GET['checkBoxName'] : ''));
if ($checkBoxName != '') {
    //do stuff here
}

This way since you can not see post info without using something like Google Chromes developer tools, its best to do it in this order so you check what is not seen before what you can see.
Hope this helps =)
EDIT:
Based on the information you gave me, i was able to come up with a way to insert only the classes you check off. Feel free to do the change this but this should work
<?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');
    }
}

?>

Good Luck =)

