0

enter image description here

What I want to do is, I have fetched data tables using JQUERY, Ajax, PHP after that selected multiple check box in the table, those selected rows of data I want to store in new database table on submit button

I don't know how to do it, I googled but there is I Don't find any such solution for this.

HTML code here

<label for="fname">insert number for select multiple check box</label>
        <input type="number" id="selectcheckbox" name="selectcheckbox">
    <table class="table table-striped table-bordered" id="tbl">
        <thead>
            <tr>
            <th>SELECT check-box</th>
            <th>QUALITY REPORT NO</th>
            <th>UID NO </th>
            <th>ITEM CODE </th>
            <th>DESCRIPTION</th>
            <th>H.NO </th>
            <th>Qty</th>
            <th>SELECTED  QTY </th>
            <th>NOTE</th>
            <th>LOCATION</th>
            <th>SELECTED  QTY hidden</th>
            <th>NOTE hidden</th>
            <th>LOCATION hidden</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
    <button type="button" class="btn btn-primary" name="submitdata" id="submitdata">submit data</button>

java-script here

<script>
$(document).ready(
    function()
        {
            var table = $('#tbl').DataTable({
                "columnDefs": 
                [{
                    orderable: false,
                    className: 'select-checkbox',
                    targets:   0
                }],
                destroy:true,
                "fnRowCallback" : 
                function(nRow, aData, iDisplayIndex)
                {
                    $("td:first", nRow).html(iDisplayIndex +1);
                    return nRow;
                },      
                'select': 
                {
                    style: 'multi',
                    selector:'td:nth-child(1)'
                },
                    'order': [[ 1, 'asc' ]],
            });
          
              $.ajax({
              type:"POST", 
              url:"fetch_data.php",  
              data:'item_code_no='+item_code_no,
                success: function(data)
                {
                  if(data['error'] == '0')
                  {
                    console.log(data);
                      
                      //set Finish Data
                      table.clear().draw();
                      for(i = 0; i < data['chemical_date'].length; i++) 
                      {
                        table.row.add([
                        data['chemical_date'][i]['mqr_id'],
                        data['chemical_date'][i]['mqr_id'],
                        data['chemical_date'][i]['uid'],
                        data['chemical_date'][i]['item_code'],
                        data['chemical_date'][i]['description'],
                        data['chemical_date'][i]['h_no'],
                        data['chemical_date'][i]['qty'],
                    
                        "<input type='text' value='' class='form-control' id='selectedqty"+i+"' onkeyup='sqty(this.id);' name='selectedqty"+i+"'/>",
                        "<input type='text' value='' class='form-control ' id='note"+i+"'  name='note"+i+"' />",
                        "<input type='text' value='' class='form-control ' id='location"+i+"'  name='location"+i+"' />",
                        '',
                        '',
                        ''
                        ]).draw(false);
                      }
                  }
                }
              })
        });
</script>
<script>
 // select check box script
    $("#selectcheckbox").on("change", function(){
        //remove selected class
        $("#example tr").removeClass("selected")
        for (let i = 0; i < $(this).val(); i++) 
        {
          //check if td there ...
          if ($("#tbl td.select-checkbox:eq(" + i + ")").length > 0) 
          {
            //add selected class..
            $("#tbl td.select-checkbox:eq(" + i + ")").closest("tr").addClass("selected") //try with `.click()` as well..
          }
        }
    })
</script>

below code is fetched data from Database using PHP

fetch_data.php

<?php
header('Content-Type: application/json');
    $response = array();
    $response = array();
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
        extract($_POST);
        //Top data
            $data = array();
            $obj = array(); 
            $sql = "SELECT DISTINCT mqr.sr_no as mqr_id,mi.item_code,mqrpd.uid,mqrpd.qty,mqrpd.h_no,mqrpd.htc_no,md.description 
                    FROM table1 mqr,table2 mqrpd,table3 mi,table4 md
                    WHERE mqr.item_id_fk=$item_code_no and mqr.id=mqrpd.quality_report_id_fk and mqr.item_id_fk=mi.id and mi.drawing_id_fk=md.id";

                $result = mysqli_query($db, $sql);
                $chemical = array();
            
                while($row = mysqli_fetch_assoc($result)) 
                {
                    //Finish Data
                    $row1 = array();
                    $row1['mqr_id'] = $row['mqr_id'];
                    $row1['item_code'] = $row['item_code'];
                    $row1['uid'] = $row['uid'];
                    $row1['qty'] = $row['qty'];
                    $row1['h_no'] = $row['h_no'];
                    $row1['htc_no'] = $row['htc_no'];
                    $row1['description'] = $row['description'];
                    array_push($chemical, $row1);
                    //Stock Data
                }
                $response['chemical_date'] = $chemical;
                //Process Data
                $response['error'] = "0";
    }
    else
        $response['error'] = "1";
        echo json_encode($response);
?>
Jason0011
  • 444
  • 1
  • 6
  • 19
  • 2
    In its current form your question is very broad as you are asking us to help you implement an entire feature as opposed to focusing on a programming problem. I can see you know how to interact with mysql database via ajax and php. So, what exactly are you struggling with? You do not know how to create a table in mysql on the fly? You do not know how to pass data you selected in the datatable? Something else? Pls be very specific and ask only **ONE** question in a post. See following discussion: https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question – Shadow Jun 09 '21 at 08:54
  • @Shadow I do not know how to pass data I selected in the datatable for insertion – Jason0011 Jun 09 '21 at 09:26
  • Then pls edit you question to focus on this problem only. Also removed php and mysql tags as passing the selected data to the server side only involves javascript and data tables. – Shadow Jun 09 '21 at 10:01
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Jun 09 '21 at 11:23
  • 1
    Check [this](https://stackoverflow.com/questions/33240409/how-to-submit-checkboxes-from-all-pages-with-jquery-datatables) post should be helpful .In your case you can iterate over `selected` trs and save value in some variable and pass them to your server via ajax. – Swati Jun 09 '21 at 11:54

0 Answers0