gpt4 book ai didi

javascript - 提交后无法取消禁用已从数据库获取其值的复选框

转载 作者:行者123 更新时间:2023-11-29 19:03:01 24 4
gpt4 key购买 nike

我需要你帮助解决我的问题。我有两个带有复选框的表,第一个是显示数据库中的所有记录(使用 MySQL),第二个表在我检查并从第一个表提交后获取数据。所以我想在之后禁用第一个表中的复选框我检查并将数据提交到第二个表,但是如果我想取消复制,我检查第二个表,这样第一个表中的复选框将被启用,第二个表中的数据将被删除(因为它只是用于存储的临时变量)另一个数据库)。在此代码中,我使用 disable 但它无法工作,问题出在哪里?

// this code for checked the checkbox that be chosen first
session_start();
if (count($_POST)){
//save choices to session
$_SESSION['select_DB']=$_POST['select_DB'];
}

function was_checked($i) {
if ($_SESSION['select_DB']){
if(in_array($i,$_SESSION['select_DB']) ) {
return "checked='checked'";
}
}
return "";
}

?>

 // this one if cancel the submit
<script>
function openCB() {
document.getElementById("balik_DB[]").disabled = false;
}
</script>


//i'm using this to disable checkbox after submit
<script>
function closeCB() {
document.getElementById("balik_DB[]").disabled = true;
}
</script>

   <!-- This code show table with checkbox that its value get from DB--> 
<tbody>


<?php
include "db_connect.php";

$sql_select = $dt_bas->prepare("SELECT id_pegawai, nm_pegawai, tmp_lahir FROM pegawai");
$sql_select->execute();

while($row = $sql_select->fetch(PDO::FETCH_ASSOC)){
$id = $row["id_pegawai"]; // id
$nm = $row["nm_pegawai"]; // employe name
$tmp = $row["tmp_lahir"]; // birthday
?>
<tr>
<td><input type="checkbox" name="select_DB[]" id="select_DB[]" value=<?php echo"$id";?> <?=was_checked($id)?>/></td>
<td><?php echo "$id";?></td>
<td><?php echo "$nm";?></td>
<td><?php echo "$tmp";?></td>
<td>
<a href="#">Ubah</a>
<a href="#">Detail</a>
</td>
</tr>

<?php } ?>
</tbody>

<!--this the second table that get its value from the first table-->
<form>

<table border="1">
<thead>
<tr>
<th></th>
<th>ID</th>
<th>Nama</th>
<th>TTL</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php
// this code for get the data that is chosen from checkbox
include "db_connect.php";

if(isset($_POST['select_DB'])){
foreach($_POST['select_DB'] as $select=>$option_id){
$sql_select2 = $dt_bas->prepare("SELECT id_pegawai, nm_pegawai, tmp_lahir FROM pegawai WHERE id_pegawai = '$option_id' ");
$sql_select2->execute();

while($row = $sql_select2->fetch(PDO::FETCH_ASSOC)){
$id = $row["id_pegawai"]; // id
$nm = $row["nm_pegawai"]; // employee name
$tmp = $row["tmp_lahir"]; // birth place
?>

<tr>
<td><input type="checkbox" id="balik_DB[]" value="<?php echo"$id";?>"/></td>
<td><?php echo"$id";?></td>
<td><?php echo"$nm";?></td>
<td><?php echo"$tmp";?></td>
<td>
<a href="#">Ubah</a>
<a href="#">Detail</a>
</td>
</tr>
<?php }
}
}
?>


</tbody>
<tfooter>
</tfooter>
</table>
<input type="submit" value="Balik" onclick="openCB()"/>
</form>

最佳答案

id="balik_DB[]" 中的问题是因为您从数据库获取数据并动态创建复选框,因此当多个复选框具有相同的“id”时会发生什么(id 应该是unique)。因此,您需要使用 class = "balik_DB[]" 而不是使用 id 。

关于javascript - 提交后无法取消禁用已从数据库获取其值的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43715517/

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