如何在Codeigniter中将值从数据表传递到控制器?

r6hnlfcb  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(131)

我正在用codeigniter编程。我有数据表来显示我的购物车。但是我不能从数据表传递值到控制器。这是我在视图上的jquery。
我想传入控制器的变量是

$id_penjualan = '8377'

这是我视图中的数据表:

function getdata() {
        var id_penjualan = $('#id_penjualan2').val();
        table = $("#table-sales").DataTable({
            "processing": true,
            "searching": false,
            "ordering": false,
            "paging":   false,
            "info":     false,
            "scrollY": "50vh",
            "scrollCollapse": true,
            "ajax": {
                // "url": `<?php echo base_url() ?>salesinvoice/get_cart_edit`,
                "url": `<?php echo base_url();?>salesinvoice/get_cart_edit`,
                "type": "GET",
                "data": {id_penjualan : id_penjualan},
            },
            "columns": [
                { "title" : "No", "render" : (data,type,row,meta) => {return meta.row + 1}, "width" : "5%" },
                { "title" : "Kode Barang", "data": "kode_barang", "orderable" : false },
                { "title" : "Nama Barang", "data": "nama_barang", "orderable" : false },
                { "title" : "Jumlah", "data": "jumlah", "orderable" : false },
                { "title" : "Satuan", "data": "satuan", "orderable" : false },
                { "title" : "Harga", "data": "harga", "orderable" : false },
                { "title" : "Diskon (%)", "data": "diskon", "orderable" : false },
                { "title" : "Diskon", "data": "total_diskon", "orderable" : false },
                { "title" : "Total", "data": "total", "orderable" : false },
                { "title" : "Action", "data": "action", "orderable" : false },
            ]
        });
    }

这是我的控制器

function get_cart_edit()
    {
        $total_diskon = 0;
        $sub = 0;
        $subtotal = 0;
        $total_dpp = 0;
        $tax_amount = 0;
        $total_akhir = 0;
        
        $id_penjualan       = $this->input->post('id_penjualan2');
        $result= $this->salesinvoice_model->getdatadummy($id_penjualan);
        // print_r($id_penjualan);exit;
        $list       = [];
        $no = 1;
        foreach ($result as $i => $items) {
            // print_r($items);exit;
            $rowidx = $items['id_penjualan_item'];
            $satuan =db_get_where('satuan', ['id_satuan' => $items['id_satuan']])->row_array();
            $no++;
            $row['no']  = $no;
            $row['kode_barang'] = $this->salesinvoice_model->getkodeitem($items['id_item']).'<input type="hidden" name="id2" id="id2" value="'.$items['id_item'].'" />';
            $row['nama_barang'] = $this->salesinvoice_model->getnamaitem($items['id_item']);
            $row['jumlah']      = $items['jumlah'];
            $row['satuan']      = isset($satuan['nama_satuan']) ? $satuan['nama_satuan'] : "";
            $row['harga']       = number_format($items['harga_jual']);
            $row['diskon1']     = (float)$items['diskon_persen'];
            $row['total_diskon']= number_format($items['total_diskon']);
            $row['total']       = number_format(($items['harga_jual'] * $items['jumlah']) - $items['total_diskon']);
            $row['action']      = '
            <button type="button" class="update_cart btn btn-warning btn-sm" onclick="editCart(\''.$rowidx.'\')"><i class="fa fa-edit"></i></button>
            <button type="button" id="'.$items['id_penjualan_item'].'" class="romove_cart btn btn-danger btn-sm"><i class="fa fa-trash-o"></i></button>
            ';
            $list[] = $row;
            $total_diskon += $items['diskon_nominal'];
            $sub = $this->cart->total();
            $subtotal = $sub - $total_diskon;
            $total_dpp = $subtotal;                 
            $tax_amount = (11 * $total_dpp) / 100;              
            $total = $total_dpp + $tax_amount;
        }
        
        $metode_pembayaran = array(
            "1" => "CASH",
            "2" => "EDC",
            "3" => "TRANSFER",
        );
        $kas = create_double($this->salesinvoice_model->getkas(),'id_kas','nama_kas');
        echo json_encode([
            "data" => $list,
            "metode_pembayaran" => $metode_pembayaran,
            "total_diskon" => $total_diskon,
            "sub" => $sub,
            "subtotal" => $subtotal,
            "total_dpp" => $total_dpp,
            "tax_amount" => $tax_amount,
            "total_akhir" => $total_akhir,
            "kas" => $kas,
        ]);
    }

这是我的模特

public function getdatadummy($id_penjualan){
        $this->db->select('*');
        $this->db->from('dummy_penjualan_item');        
        $this->db->where('id_penjualan',$id_penjualan);
        $result = $this->db->get()->result_array();
        return $result;
    }

当我运行我的代码,它的返回$id_penjualan空值。谁能帮助我。谢谢

kulphzqa

kulphzqa1#

您应该只对 AJAX 请求做以下更改:

ajax: {
        type: 'POST',
        url: '<?php echo base_url();?>salesinvoice/get_cart_edit',
        data: function(data) {
           data.id_penjualan = $('#id_penjualan2').val()
        },
        dataSrc: '',
   },

相关问题