gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:58:30 25 4
gpt4 key购买 nike

我有一个使用 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');
}
}?>

最佳答案

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

当使用$_REQUEST时,你是在说嘿看看帖子或获取变量并使用其中一个值。那么,如果您同时拥有同名的 $_POST$_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');
}
}

?>

祝你好运=)

关于php - foreach 循环插入所有复选框(选中和未选中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15435198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com