Home » Javascript » Writing the code correctly – Javascript

Writing the code correctly – Javascript

Posted by: admin October 29, 2018 Leave a comment

Questions:

Hi guys I have a table that shows all users registered on my website, no problem in selecting the checkboxes dynamically, I report the code of my table:

php:

<!--DELETE DATA CHECKBOX-->
<?php
session_start();
include '../a/connessione.php';
if(isset($_POST['delete'])){
$checkbox = $_POST['check'];
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i]; 
$db ="DELETE FROM user WHERE id='".$del_id."'";
$q = mysqli_query($connessione, $db);
}
}
?>
<!--/DELETE DATA CHECKBOX-->

table:

ID
Nome
Email
Azioni

<!--Table body-->
<?php
$i=0;
while($row = mysqli_fetch_assoc($query)){ ?>

<tbody>
<tr>
<th scope="row">
<input name="check[]" type="checkbox" id="<?php echo $row['id'] ;?>" value="<?php echo $row['id'] ;?>">
<label   for="<?php echo $row['id'] ;?>" class="label-table"></label>
</th>
<td><?php echo $row['id'] ;?><br></td>
<td><?php echo $row['nome'] ;?><br></td>
<td><?php echo $row['email'] ;?><br></td>
<td><a href="#edit<?php echo $row['id']; ?>" data-toggle="modal" class="btn btn-outline-blue btn-rounded btn-sm px-2"> <i class="fa fa-info-circle mt-0"></i></a>  
<?php include('button.php'); ?></td>
</tr>
</tbody>  
<?php } ?>   
<!--Table body-->
</table>
<!--Table-->
</div>

<hr class="my-0">

<!--Bottom Table UI-->
<div class="d-flex justify-content-between">

una foto:

enter image description here

my goal is to create a checkbox that selects all the other clicks, I bring the correct code to do this

code:

<script>
$('.js-selectall').on('change', function() {
  var isChecked = $(this).prop("checked");
  var selector = $(this).data('target');
  $(selector).prop("checked", isChecked);
});
</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="checkbox" class="js-selectall" data-target=".js-selectall1" />

<table>
  <tr>
    <td><input type="checkbox" class="js-selectall1" /> </td>
    <td><input type="checkbox" class="js-selectall1" /> </td>
    <td><input type="checkbox" class="js-selectall1" /> </td>
    <td><input type="checkbox" class="js-selectall1" /> </td>
  </tr>
  <tr>
    <td><input type="checkbox" class="js-selectall1" /> </td>
    <td><input type="checkbox" class="js-selectall1" /> </td>
    <td><input type="checkbox" class="js-selectall1" /> </td>
  </tr>
</table>

I can not write correctly and the code that allows me to create a checkbox that selects all the other clicks while having two codes written correctly, how can I do it?

Answers: