首页 > 解决方案 > 清理php中的多项选择列表

问题描述

我正在尝试在单击 addbutton 时清理使用以下 Jquery 代码插入的动态 invoiceitem POST 数组,如下所示。

<script>
        $(document).ready(function() {
            $(document).on('click', '.btnadd', function() {
                var html = '';
                html += '<tr>';
                html += '<td><input type="hidden" class="form-control product_name" name="product_name[]" readonly></td>';
                html += '<td><select class="form-control product_id" name="product_id[]" style="width: 250px";><option value="">Select Option</option> <?= H::fill_product();?></select></td>';
                html += '<td><input type="number" class="form-control inventory" name="inventory[]" readonly></td>';
                html += '<td><input type="number" class="form-control price" name="price[]" readonly></td>';
                html += '<td><input type="number" min="1" class="form-control qty"  name="qty[]" ></td>';
                html += '<td><input type="number" class="form-control total" name="total[]" readonly></td>';
                html += '<td><center><button type="button" name="remove" class="btn btn-danger btn-sm btnremove"><span class="fas fa-times"></span></button><center></td></center>';
                $('#producttable').append(html);
                //Initialize Select2 Elements
                $(".product_id").select2()
                $(".product_id").on('change', function(e) {
                    var product_id = this.value;
                    var tr = $(this).parent().parent();
                    $.ajax({
                        url: "<?=PROOT?>invoices/getproduct",
                        method: "POST",
                        data: {
                            id: product_id
                        },
                        success: function(data) {
                            //         console.log(data); 
                            tr.find(".product_name").val(data.name);
                            tr.find(".inventory").val(data.inventory);
                            tr.find(".price").val(data.price);
                            tr.find(".qty").val(1);
                            tr.find(".total").val(tr.find(".qty").val() * tr.find(".price").val());
                            calculate(0, 0);
                        }
                    })
                })
            }) // btnadd end here    
            $(document).on('click', '.btnremove', function() {
                $(this).closest('tr').remove();
                calculate(0, 0);
                $("#paid").val(0);
            }) // btnremove end here  
            $("#producttable").on("keyup change", ".qty", function() {
                var quantity = $(this);
                var tr = $(this).parent().parent();
                if ((quantity.val() - 0) > (tr.find(".inventory").val() - 0)) {
                    swal("WARNING!", "SORRY! This much of quantity is not available", "warning");
                    quantity.val(1);
                    tr.find(".total").val(quantity.val() * tr.find(".price").val());
                    calculate(0, 0);
                } else {
                    tr.find(".total").val(quantity.val() * tr.find(".price").val());
                    calculate(0, 0);
                }
            })
    
            function calculate(dis, paid) {
                var subtotal = 0;
                var tax = 0;
                var discount = dis;
                var net_total = 0;
                var paid_amt = paid;
                var due = 0;
                $(".total").each(function() {
                    subtotal = subtotal + ($(this).val() * 1);
                })
                tax = 0.05 * subtotal;
                net_total = tax + subtotal; //50+1000 =1050
                net_total = net_total - discount;
                due = net_total - paid_amt;
                $("#subtotal").val(subtotal.toFixed(2));
                $("#tax").val(tax.toFixed(2));
                $("#total").val(net_total.toFixed(2));
                $("#discount").val(discount);
                $("#due").val(due.toFixed(2));
            } // function calculate end here 
            $("#discount").keyup(function() {
                var discount = $(this).val();
                calculate(discount, 0);
            })
            $("#paid").keyup(function() {
                var paid = $(this).val();
                var discount = $("#discount").val();
                calculate(discount, paid);
            })
        });
    </script>

但是,我不能在控制器上单独清理每个 _POST[]array。我正在使用 CURTIS PARHAM 的 PHP MVC 框架。这是我的控制器,每个 Posted 数据都经过清理,我收到一个错误,因为它只在 post 数组中添加第一项。

public function addAction(){
        $invoice = new Invoices();
        if($this->request->isPost()){
            $this->request->csrfCheck();
            $arr_product_id=$_POST['product_id'];
            $arr_product_name=$_POST['product_name'];
            $arr_inventory=$_POST['inventory'];
            $arr_price=$_POST['price'];
            $arr_qty=$_POST['qty'];
            $arr_total=$_POST['total'];
//                        $arr_product_id=$this->request->get("product_id");
//                        $arr_product_name=$this->request->get("product_name");
//                        $arr_inventory=$this->request->get("inventory");
//                        $arr_price=$this->request->get("price");
//                        $arr_qty=$this->request->get("qty");
//                        $arr_total=$this->request->get("total");
            $invoice->customer_name = $this->request->get("customer_name");
            $invoice->valid_date = $this->request->get("valid_date");
            $invoice->subtotal = $this->request->get("subtotal");
            $invoice->tax = $this->request->get("tax");
            $invoice->discount = $this->request->get("discount");
            $invoice->total = $this->request->get("total");
            $invoice->paid = $this->request->get("paid" );
            $invoice->due = $this->request->get("due");
            $invoice->payment_type = $this->request->get("payment_type");
            $invoice->save();
            foreach ($arr_product_id as $key =>$value){
                $rem_qty = $arr_inventory[$key]-$arr_qty[$key];
                if($rem_qty<0){
                    Session::addMsg('danger','Order is not Compete! Remaining quantity is less than Zero');
                }else{
                    $product = Products::findById((int)$arr_product_id[$key]);
                    $product->inventory = $rem_qty;
                    $product->save();
                }
                $invoice_Items = new InvoiceItems();  
                $invoice_Items->invoice_id = $invoice->id;
                $invoice_Items->product_id = $arr_product_id[$key];
                $invoice_Items->product_name = $arr_product_name[$key];
                $invoice_Items->qty=$arr_qty[$key];
                $invoice_Items->price = $arr_price[$key];
                $invoice_Items->valid_date = $invoice->valid_date;
                $invoice_Items->save();
            }  
            //redirect
            Session::addMsg('success','New Invoice Added and Saved Successfully!');
//            Router::redirect('invoices/index');
        }
        $this->view->invoice = $invoice;
        $products = Products::getProductsByInvoiceId($invoice->id);
        $this->view->products = $products;
        $this->view->displayErrors = $invoice->getErrorMessages();
        $this->view->setLayout('admin');
        $this->view->render('invoices/add');
    }

标签: php

解决方案


推荐阅读