php 如果有5个复选框,我不能随机取消选中复选框,只能逐个取消选中

fiei3ece  于 2023-02-03  发布在  PHP
关注(0)|答案(1)|浏览(170)

我取消选中复选框,数据库值从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);
?>
zhte4eai

zhte4eai1#

若要随机取消选中复选框,可以随机排列复选框索引数组,然后遍历该数组以随机顺序取消选中复选框。

// Get all the checkboxes
var checkboxes = document.querySelectorAll('input[type="checkbox"]');

// Create an array of indices
var indices = [];
for (var i = 0; i < checkboxes.length; i++) {
  indices.push(i);
}

// Shuffle the indices
indices = shuffleArray(indices);

// Uncheck the checkboxes in a random order
for (var i = 0; i < indices.length; i++) {
  checkboxes[indices[i]].checked = false;
}

// Function to shuffle an array
function shuffleArray(array) {
  for (var i = array.length - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }
  return array;
}

取消选中复选框后,可以使用JavaScript和PHP相应地更新数据库值。

相关问题