0

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);
}
2
  • 1
    @RiggsFolly i have updated my code. Can you please help me to do this? Need to update multiple rows in mysql data when click the save button each line update the row Commented Aug 23, 2021 at 12:40
  • Hello and welcome to StackOverflow. please take a moment and read this article stackoverflow.com/help/how-to-ask about how to asking questions also read this article stackoverflow.com/help/minimal-reproducible-example about how to ask a good question with minimum requirement. Commented Aug 24, 2021 at 7:08

1 Answer 1

0

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
        
        }
Sign up to request clarification or add additional context in comments.

1 Comment

can u plz help me to sort this out? stackoverflow.com/questions/69456098/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.