Home > Mobile >  How to update multiple table rows into mysql table when click save button using Jquery and PHP?
How to update multiple table rows into mysql table when click save button using Jquery and PHP?

Time:09-17

I have a myql table name - invoice_details

invoice_number received_amount receiptID
1000               0.00         
1001                0.00
1005                0.00

I have a html table enter image description here

When clicking the save button, update invoice_details table with received amount and receiptID where invoice number in the html table row (1001,1005) Multiple rows will be there in html table.

appending this html table code:

$('#invoicelist_receipt').find('tbody').remove();
$.each($("input[name='myTextEditBox[]']:checked"), function() {
    var data = $(this).parents('tr:eq(0)');
           
    //  values  = $(data).find('td:eq(0)').text()   ","   $(data).find('td:eq(1)').text()   ","   $(data).find('td:eq(2)').text()   ",";   
    var t1 = $(data).find('td:eq(0)').text();//invoice date
    var t2 = $(data).find('td:eq(1)').text();//invoice no
    var t3 = $(data).find('td:eq(2)').text();//invoice amt
    trtoggle  = "<tr><td class=''>"   t1   "</td><td name='invoice_no_receipt[]' class='invoice_no_receipt'>"   t2   "</td><td class=''>"   t3   "</td><td class=''><input class='form-control invoice_amt_receipt' type='number' data-type='invoice_amt_receipt' id='invoice_amt_receipt_1' name='invoice_amt_receipt[]' for='1'/></td></tr>";    
    //values.push({ 'invoicedate':$(data).find('td:eq(0)').text(), 'invoiceno':$(data).find('td:eq(1)').text() , 'invoiceamount':$(data).find('td:eq(2)').text()});             
});
$("#invoicelist_receipt").last().append(trtoggle);  

when button clicks:(creating a new receipt)

var invoice_no_receipt  = []; //where invoice no = 1000,1001,1005 etc..
var invoice_amt_receipt  = [];//this received amount i have to update into database - invoice details table
        
$('.invoice_no_receipt').each(function() {
    invoice_no_receipt.push($(this).val());
});

$('.invoice_amt_receipt').each(function() {
    invoice_amt_receipt.push($(this).val());
});
    
$.ajax({
    url: base_url   "index.php/welcome/savereciptfinal/",
    type: "POST",

    data: {
        "getinvnumber": invoice_no_receipt,
        "getinv_recived_amount": invoice_amt_receipt
    },
    success: function(data) {

    }
});

PHP Codeigniter code

public function savereciptfinal()
    $value2 = "0001"; //autogenerate number
    $value  = $value2;
    $data = array(
        'rece_No' => $value
    );

    $insert_id = 0;
    if ($this->db->insert("receipt_details", $data)) {
        $insert_id = $this->db->insert_id();
    }

    $data2 = array(
                'rece_Amt' => $this->input->post('getrece_amt'),
                'receipt_ID' => $value2; //the above auto generated number i need to update invoice_details for column receiptID
            );
    $this->db->where('invoice_No ', $inv_id);
    $this->db->update('invoice_details', $data2);
}

CodePudding user response:

You can get ideas from this code and sync it with your own code

First clear this script:

$('#invoicelist_receipt').find('tbody').remove();

$.each($("input[name='myTextEditBox[]']:checked"), function() {
    var data = $(this).parents('tr:eq(0)');  
    var t1 = $(data).find('td:eq(0)').text();//invoice date
    var t2 = $(data).find('td:eq(1)').text();//invoice no
    var t3 = $(data).find('td:eq(2)').text();//invoice amt
    trtoggle  = "<tr><td class=''>"   t1   "</td><td name='invoice_no_receipt[]' class='invoice_no_receipt'>"   t2   "</td><td class=''>"   t3   "</td><td class=''><input class='form-control invoice_amt_receipt' type='number' data-type='invoice_amt_receipt' id='invoice_amt_receipt_1' name='invoice_amt_receipt[]' for='1'/></td></tr>";    
                
});
$("#invoicelist_receipt").last().append(trtoggle); 

 

Set your save button id instead of 'your_save_button_id'

You can see how send your invoice details in js

$(document).on('your_save_button_id', 'click', function(){ 

    var invoice_no_receipt  = []; 
    
    $('.invoice_no_receipt').each(function() {
    
        // Json format to add product invoice detail
        var invoice_detail = new Object();

        // Get invoice number
        invoice_detail.no = $(this).text();
        
        // Get parent tr
        var parent_tr = $(this).closest('tr');

        // Get amount
        invoice_detail.amt = $(parent_tr).find('invoice_amt_receipt').val();

        // Add invoice detail to invoice_no_receipt
        invoice_no_receipt.push(invoice_detail);

    });
        
    $.ajax({
        url: base_url   "index.php/welcome/savereciptfinal/",
        type: "POST",
    
        data: {
            "invoice_no_receipt": invoice_no_receipt
        },
        success: function(data) {
    
        }
    });
})

You can see how can get your invoice detail in PHP

IN Your PHP code in ajax function

// Get invoice_no_receipt
        $invoice_no_receipt = $_POST('invoice_no_receipt');
    
        foreach( $invoice_no_receipt as $item )
        {
            // Get invoice number
            $invoice_no = intval($item['no']);
            
            // Get invoice amount
            $invoice_amt = floatval($item['amt']);
            
            // Update your invoice in db one by one and by use from these variable
            // Your update function
        
        }
  • Related