0

我有一个使用 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>

现在,当我使用 myuniversity_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');
}
}?>
4

2 回答 2

1

我喜欢@Martin 的回答。我唯一想改变的是$_REQUEST

使用时,$_REQUEST您是在说嘿查看帖子或获取变量并使用任一值。那么,如果您同时拥有同名的 a$_POST和 a变量会发生什么?$_GET一个会被使用,而另一个不会。

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

$checkBoxName = (isset($_POST['checkBoxName']) ? $_POST['checkBoxName'] : (isset($_GET['checkBoxName']) ? $_GET['checkBoxName'] : ''));
if ($checkBoxName != '') {
    //do stuff here
}

通过这种方式,如果不使用 Google Chrome 开发者工具之类的工具,您将无法查看帖子信息,因此最好按此顺序进行操作,以便在您可以看到之前检查未看到的内容。

希望这会有所帮助=)

编辑:

根据您给我的信息,我能够想出一种方法来仅插入您检查的类。随意进行更改,但这应该可以

<?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');
    }
}

?>

祝你好运=)

于 2013-03-15T15:05:24.057 回答
0

您可以使用 isset 函数来检查复选框是否被选中。

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

    $variable = $_REQUEST['checkBoxName'];
    }

使用此方法,您只会得到具有值的复选框。

希望这可以帮助

于 2013-03-15T14:50:35.930 回答