简体   繁体   English

使用 Jquery 和 PHP 单击保存按钮时,如何将多个表行更新为 mysql 表?

[英]How to update multiple table rows into mysql table when click save button using Jquery and PHP?

I have a myql table name - invoice_details我有一个 myql 表名 - invoice_details

invoice_number received_amount receiptID
1000               0.00         
1001                0.00
1005                0.00

I have a html table我有一张 html 表在此处输入图像描述

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.单击保存按钮时,使用收到的金额和 receiptID 更新 invoice_details 表,其中 html 表行中的发票编号 (1001,1005) html 表中将有多个行。

appending this html table code:附加此 html 表代码:

$('#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 PHP Codeigniter 代码

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

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'设置您的保存按钮 ID 而不是“your_save_button_id”

You can see how send your invoice details in js您可以在 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您可以在 PHP 中查看如何获取您的发票详细信息

IN Your PHP code in ajax function IN 您的 PHP 密码 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
        
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM