Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If there is like 5 checkboxes, I can't uncheck the checkboxes randomly. I can only uncheck one by one

I uncheck the checkboxes, database value got update to 0 from 1. But I can’t uncheck the checkboxes randomly. I can only uncheck one by one. Example, there is like 5 results, I can uncheck from 1 to 5 in order. I want to do is uncheck randomly like 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>`

Below is correlationwafer_result.php.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?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>

Below is from 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);
?>

>Solution :

To randomly uncheck the checkboxes, you can shuffle an array of checkbox indices and then iterate through the array to uncheck the checkboxes in a random order.

// 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;
}

After unchecking the checkboxes, you can use JavaScript and PHP to update the database values accordingly.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading