Solution 1 :

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());
  });

Solution 2 :

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.

Problem :

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.

Comments

Comment posted by Harsimranjit Singh

if you only want the value of checkbox then you no need to write that much query code. you can simply get the values of the checkbox by $(‘.Dptadmin’).on(‘click’, function(){ console.log($(this).val()); })

Comment posted by CodeBug

hi @HarsimranjitSingh. thank you for your response. I’m kind of new to this. can you please provide little code about that.

Comment posted by CodeBug

yes, this works correctly. but my requirement is I need to get the id of the particular row of which checkbox is checked. because I need to store that in my MySQL database, for that I have written ajax call. so here with the checkbox clicked i want the related userid also which is placed in that td

Comment posted by Harsimranjit Singh

for that first of all add ID in your HTML TR code, your

tag doesn’t have any ID you need to give it a id and then your problem can be solved by using var row_id = $(this).closest(‘tr’).attr(‘id’);

Comment posted by Anant – Alive to die

still i will say stop using

By