清理php中的多项选择列表

清理php中的多项选择列表,php,Php,我正在尝试清理使用以下Jquery代码插入的动态invoiceitem POST数组,方法是单击addbutton,如下所示 <script> $(document).ready(function() { $(document).on('click', '.btnadd', function() { var html = ''; html += '<tr>';

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

<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>

$(文档).ready(函数(){
$(文档).on('单击','.btnadd',函数(){
var html='';
html+='';
html+='';
html+=“选择选项”;
html+='';
html+='';
html+='';
html+='';
html+='';
$('#producttable')。附加(html);
//初始化Select2元素
$(“.product_id”)。选择2()
$(“.product_id”)。关于('change',函数(e){
var product_id=此值;
var tr=$(this.parent().parent();
$.ajax({
url:“发票/getproduct”,
方法:“张贴”,
数据:{
标识:产品标识
},
成功:功能(数据){
//控制台日志(数据);
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());
计算(0,0);
}
})
})
})//b在这里结束
$(文档).on('单击','.btnremove',函数(){
$(this).closest('tr').remove();
计算(0,0);
美元(“#已付”).val(0);
})//b删除此处的结尾
$(“#producttable”).on(“键控更改”,“数量”,函数(){
变量数量=$(此);
var tr=$(this.parent().parent();
if((quantity.val()-0)>(tr.find(“.inventory”).val()-0)){
swal(“警告!”,“对不起!这么多数量没有货”,“警告”);
数量val(1);
tr.find(“.total”).val(quantity.val()*tr.find(“.price”).val());
计算(0,0);
}否则{
tr.find(“.total”).val(quantity.val()*tr.find(“.price”).val());
计算(0,0);
}
})
功能计算(dis,付费){
var小计=0;
var税=0;
风险值贴现=贴现;
var净值合计=0;
已支付风险值\金额=已支付;
到期风险价值=0;
$(“.total”)。每个(函数(){
小计=小计+($(this).val()*1);
})
税=0.05*小计;
净总额=税金+小计;//50+1000=1050
净总额=净总额-折扣;
到期=总净额-已付金额;
美元(“#小计”).val(小计固定(2));
$(“#tax”).val(tax.toFixed(2));
$(“#总计”).val(净总计.toFixed(2));
美元(“#折扣”).val(折扣);
美元(“#到期日”).val(到期日固定(2));
}//函数计算到此结束
$(“#折扣”).keyup(函数(){
var折扣=$(this.val();
计算(折扣,0);
})
$(“#支付”).keyup(函数(){
var paid=$(this.val();
var折扣=$(“#折扣”).val();
计算(折扣,已付);
})
});
但是,我无法在控制器上单独清理每个_POST[]数组。 我正在使用CURTIS PARHAM的PHP MVC框架。 这是我的控制器,每个发布的数据都被清理,我得到一个错误,因为它只是在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');
    }
公共函数addAction(){
$invoice=新发票();
如果($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(“库存”);
//$arr_price=$this->request->get(“price”);
//$arr_qty=$this->request->get(“数量”);
//$arr_total=$this->request->get(“total”);
$invoice->customer\u name=$this->request->get(“customer\u name”);
$invoice->valid_date=$this->request->get(“valid_date”);
$invoice->subtotal=$this->request->get(“小计”);
$invoice->tax=$this->request->get(“tax”);
$invoice->折扣=$this->request->get(“折扣”);
$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\u product\u id为$key=>$value){
$rem_数量=$arr_存货[$key]-$arr_数量[$key];
如果($rem\u数量存货=$rem\u数量;
$product->save();
}
$invoice_Items=新发票项();
$invoice\u Items->invoice\u id=$invoice->id;
$invoice\u Items->product\u id=$arr\u product\u id[$key];
$invoice\u项目->产品_