Home » Jquery » jquery – How to get the checkbox value in php html

jquery – How to get the checkbox value in php html

Posted by: admin February 22, 2020 Leave a comment

Questions:

I have a table showing database values as shown here:

<?php
       while ($single = $output->fetch_assoc()):?> 
  <tr>
    <td> <?php echo $single['Dptname']; ?></td>
    <td> <?php echo $single['firstname']; ?> </td>
    <td> <?php echo $single['lastname']; ?> </td>
    <td> <?php echo $single['email']; ?> </td>
    <td> <?php echo $single['phnumber']; ?> </td>
    <td> <?php echo $single['provider']; ?> </td>
     <td style="display: none"> <?php echo $single['id']; ?></td>
    <td><input type="checkbox" class="Dptadmin" id="Dptadmin" value="1"></td>
    <td><input type="checkbox" class="superuser" id="superuser" value="1"></td>
    <td><button type="button" class="btn b1 editbtn" data-toggle="modal" value="" data-target="#myModal"> <i class="fa fa-pencil-square-o">  </i> </button>
        <a href="process.php?delete=<?php echo $single['id'];?>" class="b2"><i class="fa fa-trash-o"></i> </a>
    </td>
  </tr>
    <?php endwhile;?>

jQuery:

<script>
 $(document).ready(function (){
    $('.editbtn').on('click', function(){
        $('#editmodal').modal('show');
        $tr = $(this).closest('tr');
        var data = $tr.children('td').map(function(){
          return $(this).text();
        }).get();
        console.log(data);
        $('#Dptname').val(data[0]);
        $('#firstname').val(data[1]);
        $('#lastname').val(data[2]);
        $('#email').val(data[3]);
        $('#phnumber').val(data[4]);
        $('#provider').val(data[5]);
        $('#userid').val(data[6]);
        $('#userid').val(data[6]);
        $('#Dptadmin').val(data[7]);
    });
 }); 

Tried:-

 $('.Dptadmin').on('click', function(){
     $tr = $(this).closest('tr');
        var data = $tr.children('td').map(function(){
          return $(this).text();
        }).get();
        console.log(data);
        $('#userid').val(data[6]);
        var val = $(this).find(":checkbox['<?php echo $single['Dadmin']; ?>']").val();
        console.log(val);

As above jQuery, from console.log(data) I’m getting the value of selected TD, but not getting checkbox value. Thank you in advance.

How to&Answer:

if you only want to get the value of your checkbox when it is clicked then use the below code

  $('.Dptadmin').on('click', function(){
    console.log($(this).val());
  });

Answer:

I got the solution on my own. thank you for your support your ideas are made me think beyond. here is my solution.

 $('.Dadmin').on('click', function(){
         $tr = $(this).closest('tr');
        var data = $tr.children('td').map(function(){
          return $(this).text();
        }).get();
        var userid = data[6];
        var admin = $('#Dadmin').val();
        $.ajax({
        type: 'POST',
        url: 'makeadmin.php',
        data: {
            userid : userid,
            admin : admin,
        },
        success: function(data){
            console.log(data.length);
        },
        });
    })

html

 <tr>
    <td> <?php echo $single['Dptname']; ?></td>
    <td> <?php echo $single['firstname']; ?> </td>
    <td> <?php echo $single['lastname']; ?> </td>
    <td> <?php echo $single['email']; ?> </td>
    <td> <?php echo $single['phnumber']; ?> </td>
    <td> <?php echo $single['provider']; ?> </td>
     <td style="display: none"> <?php echo $single['id']; ?></td>
    <td><input type="checkbox" class="Dadmin" id="Dadmin" value="1"></td>
    <td><input type="checkbox" class="superuser" id="superuser" value="1"></td>
    <td><button type="button" class="btn b1 editbtn" data-toggle="modal" value="" data-target="#myModal"> <i class="fa fa-pencil-square-o">  </i> </button>
        <a href="process.php?delete=<?php echo $single['id'];?>" class="b2"><i class="fa fa-trash-o"></i> </a>
    </td>
  </tr>

here only i changed $('#userid').val(data[6]); to var userid = data[6];

hope so this will help somebody. thank you all once.

Answer:

try it

var myCheckboxes = new Array();
   $("input:checked").each(function() {
    myCheckboxes.push($(this).val());
  });