我有一个使用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>
现在,当我使用university_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');
}
}?>
答案 0 :(得分:1)
我喜欢@Martin的回答。我唯一想要改变的是$_REQUEST
使用$_REQUEST
时,你会说他们看一个帖子或获取变量并使用任何一个值。那么,如果同时拥有同名的$_POST
和$_GET
变量,会发生什么?一个将被使用,而另一个不会。
因此,让我们以不同的方式使用相同的代码。
$checkBoxName = (isset($_POST['checkBoxName']) ? $_POST['checkBoxName'] : (isset($_GET['checkBoxName']) ? $_GET['checkBoxName'] : ''));
if ($checkBoxName != '') {
//do stuff here
}
这种方式,因为您无法在不使用Google 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');
}
}
?>
祝你好运=)
答案 1 :(得分:0)
您可以使用isset函数检查是否选中了复选框。
if (isset($_REQUEST['checkBoxName']));
{
$variable = $_REQUEST['checkBoxName'];
}
使用此方法,您只会获得具有值的复选框。
希望这有帮助