我取消选中复选框,数据库值从1更新为0。但我不能随机取消选中复选框。我只能逐个取消选中。例如,有5个结果,我可以按顺序取消选中1到5。我想做的是随机取消选中,如5 2 3 1。
Result
</div>
<div id="result" style="display: inline-table; margin-left: 150px; margin-top: 22px;">
<?php
include("correlationwafer_result.php");
?></div>
<!--div id="result" ></div-->
<div class="col-sm-10">
</div>`
下面是correlationwafer_result.php.
<?php
// ini_set("memory_limit","512M");
include("_dbconn.php");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/db_config.inc");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/standard_defines.inc");
session_start();
$productlotid = isset ($_GET['productlotid'])? $_GET['productlotid']:'';
$zone_enable = isset ($_GET['zone_enable'])? $_GET['zone_enable']:'';
$zone_enablee = isset ($_GET['zone_enablee'])? $_GET['zone_enablee']:'';
//$sql1 = "Update * FROM productdb.tbl_correlationwafer WHERE lotid = '$productlotid' ORDER BY lotid and zone_enable='0'";
$sql = "SELECT * FROM productdb.tbl_correlationwafer WHERE lotid = '$productlotid' ORDER BY product asc, zone asc";
$result1 = mysqli_query($conn,$sql);
$row_cnt = mysqli_num_rows($result1);
echo '<table class="table table-bordered table-striped">';
echo "<thead>
<tr>
<th>Lot ID</th>
<th>Product</th>
<th>Zone</th>
<th>Enable</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result1))
{
echo '<tr>';
echo '<td>'.$row['lotid'].'</td>';
echo '<td>'.$row['product'].'</td>';
echo '<td>'.$row['zone'].'</td>';
echo "<td><input type='checkbox' name='zone_enable' id='zone_enablee' value='1' onchange='Submitt(\"".$row['zone']."\",\"".$row['lotid']."\")'";
if($row['zone_enable']==1) {
echo "checked='checked'";
}
echo "></td>";
echo '</tr>';
}
echo '</table>';
?>
<script type="text/javascript">
function Submitt(zone,productlotid){
var xhr = new XMLHttpRequest();
var checkbox = document.getElementById("zone_enablee");
var value = checkbox.checked ? 1 : 0;
xhr.open("GET", "test_1.php?zone="+zone+"&value="+value+"&productlotid="+productlotid, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText);
if(!checkbox.checked){
console.log("Record updated successfully");
}
}
}
xhr.send();
}
</script>
下面的代码来自test_1.php。
<?php
// ini_set("memory_limit","512M");
include("_dbconn.php");
$zone = $_GET['zone'];
$value = $_GET['value'];
$productlotid = $_GET['productlotid'];
$sql = "UPDATE productdb.tbl_correlationwafer SET zone_enable = '$value' WHERE lotid = '$productlotid' AND zone = '$zone'";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
echo json_encode($response);
mysqli_close($conn);
?>
1条答案
按热度按时间zhte4eai1#
若要随机取消选中复选框,可以随机排列复选框索引数组,然后遍历该数组以随机顺序取消选中复选框。
取消选中复选框后,可以使用JavaScript和PHP相应地更新数据库值。