How to insert the dynamic table row values into mysql database when checking the checkbox in jquery

Hi Everybody, Please help me. Thanks in advance. Actually i have a html table with dynamic rows through for looping and submit button at the bottom. At the beginning of the row there is a checkbox , so how can i insert only the particular checked checkboxes rows values into mysql database. For example if i check first row and third row then, only that particular row datas should insert into mysql database.
My current code is inserting all the table rows into mysql database, why because i am using each function in my jquery code. So please help me how can i get only the selected row values to insert into database when checking the checkboxes and clicking submit button. In my code the php syntax are in cakephp just i need to pass only the selected checkbox rows
My code as below:

<!DOCTYPE html>
<html lang="en">
<head>   
   
<script>
$('#orderSave').on('click', function(e) {    

        e.preventDefault();          
       
       $('.rowCont tr').each(function(row, tr){ 

        var TableData = new Array();

     
        TableData[row] = {   
                  
                   
                  "userid" : $('#userid').text(), 
                  "user_name" : $('#user').text(),   
                  "date" : $(tr).find('td:eq(3)').text(),   
                  "exam" : $(tr).find('td:eq(4)').text(),
                  "venue" : $(tr).find('td:eq(5)').text(),
                  "price" : $(tr).find('td:eq(6)').text(),
                  "total_orders" : $(tr).find('td:eq(7)').text(), 
                  "amount" : $(tr).find('td:eq(8)').text(), 
                  //"orders_completion" : $(tr).find('.chk')[0].checked     
                  "orders_completion" : $('#checked').val()    

              }

          TableData = JSON.stringify(TableData);            
          console.log(TableData);
          
          //alert(TableData);          

          $.ajax({  
            
           type : "POST",    
           url : "/invl_exams/ordercompletion",                  
           cache : "false",
           data :  {data:TableData},            
           success : function(result){  

             console.log(result);                               

           } 

          
          }); 

        });

      });


  });  

  </script>         
</head>
<body>
  <p>&nbsp;</p>
  <?php $uid = $this->Session->read('Auth.User.id'); ?>
  <p style="font-size:25px;text-align:left;padding: 0px 0px 0px 275px">User's Order List</p> 
 <div class="container">
   <div class="row"> 
    <div class="col-md-10">
     <div><b>Name : </b><?php echo $usersadmins[0]['carts']['username']; ?>
      <input type="hidden" name="hidename" value="<?php echo $usersadmins[0]['carts']['username']; ?>">
     </div>  
     <div>&nbsp;</div>
    <div>
    <form role="form" accept-charset="utf-8" method="post" id="examForm"  action="/invl_exams/users/orderCheck">    
     <table class="table table-bordered rowCont" id="sourcetable">    
      <tr>
       <!--<th>S.No</th>-->
       <th>Order Completion</th>
       <th>Exam Date</th>
       <th>Exam Name</th>
       <th>Venue</th>
       <th>Price / Course</th>
       <th>No of Orders</th>
       <th>Amount</th>
       
       
      </tr>
      <?php
        $j = 1; 
        for($i=0;$i<count($usersadmins);$i++)    
        {        

      ?>
      <tr>
        <!--<td><?php //echo $j++;?></td>--> 
        <td>       
          <input type="checkbox" name="chk[]" id="checked" class="chk" value="1">      
        </td>
        <td style="display:none" id="userid"><?php echo $usersadmins[0]['carts']['userid']; ?></td>
        <td style="display:none" id="user"><?php echo $usersadmins[0]['carts']['username'];?></td>   
        <td id="date"><?php echo $usersadmins[$i]['carts']['date']; ?></td> 
        <td id="exam"><?php echo $usersadmins[$i]['carts']['exam_name']; ?></td>
        <td id="venue"><?php echo $usersadmins[$i]['carts']['venue']; ?></td>
        <td id="price"><?php echo "$".$usersadmins[$i]['carts']['price']; ?></td>
        <td id="torders"><?php echo $usersadmins[$i]['carts']['noOf_orders']; ?></td>    
        <td id="amount"><?php echo "$".$usersadmins[$i]['carts']['amount']; ?></td>             
         
      </tr> 
     
      <?php } ?>  
      <tr>
        <td colspan="6">&nbsp;</td> 
        <td><button type="button" class="btn btn-success btn-xs" id="orderSave">Submit</button></td>
      </tr>
 </table>
 </div>   
</div>
</div>
</div>
</body>
</html>

Can you post your PHP code?

I would think there are two approaches - either modify your Ajax submit function to only submit those that are checked, or modify your PHP to check the checkbox field (which I think is the checkbox) and only write those into the database. When you pass the data into the PHP code, is the status of each checkbox coming through correctly?

Thanks for your reply, I got the things and i have done it. I have written a if condition to only for the check box is checked then that particular rows should insert. I have done as below

<script>

$('#orderSave').on('click', function(e) {    

        e.preventDefault();

        alert('Order Completion check Done');          
       
       $('.rowCont tr').each(function(row, tr){  
         
       if($(this).find('input').is(':checked')) {  // I have added the if condition here 

        var TableData = new Array(); 

     
        TableData[row] = {      
                  
                   
                  "userid" : $('#userid').text(),  
                  "user_name" : $('#user').text(),   
                  "date" : $(tr).find('td:eq(3)').text(),    
                  "exam" : $(tr).find('td:eq(4)').text(),
                  "venue" : $(tr).find('td:eq(5)').text(),
                  "price" : $(tr).find('td:eq(6)').text(),
                  "total_orders" : $(tr).find('td:eq(7)').text(), 
                  "amount" : $(tr).find('td:eq(8)').text(), 
                  //"orders_completion" : $(tr).find('.chk')[0].checked     
                  "orders_completion" : $('#checked').val()    

              }

          TableData = JSON.stringify(TableData);               
          console.log(TableData);
          
          //alert(TableData);           

          $.ajax({  
            
           type : "POST",    
           url : "/invl_exams/ordercompletion",                   
           cache : "false",
           data :  {data:TableData},            
           success : function(result){  

             console.log(result);                                

           } 

          
          });

         } // Here for the checkbox if condition 

        }); // each function

      }); // clicking orderSave button     


  });  

  </script>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.