繁体   English   中英

使用ajax在数据库中保存具有相同名称的多个字段

[英]save multiple fields with same name in database using ajax

我想保存具有多个行的表,当我尝试保存数据时,该行是动态生成的,只有第一个字母保存在数据库中。 当我尝试保存下一行时,它在控制台中显示500错误。 我想将所有行的所有数据一一保存在数据库中,如何实现呢?

这是我的代码:

 <!--    <form action="" method="POST">  -->
                    <?php $data = array(
                        'id'=>'inventory_form', 
                        ); ?>
                    <?php  echo form_open('main/store',$data); ?>
                   <div class="table-responsive">
                        <table class="table table-bordered table-hover" id="inv_tbl">
                            <thead>
                                <th>No</th>
                                <th>~</th>
                                <th>Barcode</th>
                                <th>Product Name</th>
                                <th>SM</th>
                                <th>SPL</th>
                                <th>Quantity</th>
                                <th>Price</th>
                                <th>Discount</th>
                                <th>Amount</th>
                                <th><input type="button" value="+" id="add" class="btn btn-primary add_inv_row"></th>
                            </thead>
                            <tbody class="detail">
                                <tr>
                                    <td class="no">1</td>
                                    <td class="text-center"><input type="checkbox" id="till_check" class="is_checked" name="till_check[]" value="Bike"></td>

                                    <td>
                                        <select name="barcode[]" id="barcode" class="form-control selectpicker barcode" data-live-search="true">
                                        <option value="">Please select a barcode</option>
                                        <?php  foreach($barcodes as $barcode) :?>
                                        <option value="<?php echo $barcode->barcode; ?>"><?php echo $barcode->barcode; ?></option>
                                        <?php endforeach; ?>
                                        </select>
                                    </td>
                                    <td><input type="text" id="brcode_product" class="form-control productname" name="productname[]"></td>
                                    <td>
                                        <select name="sm[]" id="sm" class="form-control selectpicker sm" data-live-search="true">
                                        <option value="">Please select a Employee code</option>
                                        <?php  foreach($employee_codes as $employee_code) :?>
                                        <option value="<?php echo $employee_code->emp_code; ?>"><?php echo $employee_code->emp_code; ?></option>
                                        <?php endforeach; ?>
                                        </select>
                                    </td>
                                    <td><input type="text" id="spl" class="form-control spl" name="spl[]"></td>
                                    <td><input type="text" id="quantity" class="form-control quantity" name="quantity[]"></td>
                                    <td><input type="text" id="price" class="form-control price" name="price[]"></td>
                                    <td><input type="text" id="discount" class="form-control discount" name="discount[]"></td>
                                    <td><input type="text" id="amount" class="form-control amount" name="amount[]"></td>
                                    <td><a href="#" class="remove">Delete</a></td>

                                </tr>
                            </tbody>
                            <tfoot>
                                <th></th>
                                <th></th>
                                <th></th>
                                <th></th>
                                <th></th>
                                <th></th>
                                <th></th>
                                <th></th>

                                <th class="text-center"><button class="btn btn-success" id="save_btn" type="submit" name="sale_submit_btn">Save</button></th>
                                <th style="text-align:center;" class="total">0<b></b></th>

                            </tfoot>

                        </table>




                    </div> 



                    <?php echo form_close(); ?>
                    <!--</form>-->

我的Javascript代码:-

function total() {
    var t = 0;
    $('.amount').each(function (i, e) {
        var amt = $(this).val() - 0;
        t += amt;
    });
    $('.total').html(t);
}

function addnewrow() {
    var n = ($('.detail tr').length - 0) + 1;

    var tr = '<tr>' +
        '<td class="no">' + n + '</td>' +
        '<td><input type="checkbox" class="till_check" name="till_check[' + till_check_counter + ']" id="till_check[' + till_check_counter + ']"></td>' +
        '<td><select class="form-control barcode selectpicker  dyselect_' + product_barcode_counter + '" data-live-search="true" name="barcode[' + product_barcode_counter + ']" id="barcode[' + product_barcode_counter + ']">' + '<option>Please select a bar code</option>' + '</select></td>' +
        '<td><input type="text" class="form-control prdctn_' + product_name_counter + ' productname"  id="brcode_product['+product_name_counter+']"  name="productname[' + product_name_counter + ']" id="productname[' + product_name_counter + ']"></td>' +
        '<td><select class="form-control selectpicker dysm_' + sm_counter + ' sm " data-live-search="true" name="sm[' + sm_counter + ']" id="sm[' + sm_counter + ']">' + '<option>Please select a Employee code</option>' + '</select></td>' +
        '<td><input type="text" class="form-control spl" name="spl[' + spl_counter + ']" id="spl[' + spl_counter + ']"></td>' +
        '<td><input type="text" class="form-control quantity" name="quantity[' + product_quantity_counter + ']" id="quantity[' + product_quantity_counter + ']"></td>' +
        '<td><input type="text" class="form-control price" name="price[' + product_price_counter + ']" id="price[' + product_price_counter + ']"></td>' +
        '<td><input type="text" class="form-control discount" name="discount[' + product_discount_counter + ']" id="discount[' + product_discount_counter + ']"></td>' +
        '<td><input type="text" class="form-control amount" name="amount[' + product_amount_counter + ']" id="amount[' + product_amount_counter + ']"></td>' +
        '<td><a href="#" class="remove">Delete</td>' +
        '</tr>';
    $('.detail').append(tr);







    //increamenting the counter
    ++product_barcode_counter;
    ++till_check_counter;
    ++product_name_counter;
    ++product_quantity_counter;
    ++sm_counter;
    ++spl_counter;
    ++product_price_counter;
    ++product_discount_counter;
    ++product_amount_counter;

    //setting the validation rules for every product attribute by calling the function 
    createValidation();
    get_barcodes();
    get_employee_codes();







}

    $('#save_btn').on('click', function () {
        var barcode = $('#barcode  option:selected').val();
        var productname = $('#brcode_product').val();
        var sm = $('#sm  option:selected').val();
        var spl = $('#spl').val();
        var quantity = $('#quantity').val();
        var price = $('#price').val();
        var discount = $('#discount').val();
        var amount = $('#amount').val();


        var dataString = 'barcode=' + barcode + '&productname=' + productname + '&sm=' + sm + '&spl=' + spl + '&quantity' + quantity + '&price=' + price + '&discount=' + discount + '&amount=' + amount;
        $.ajax({
        url: "http://localhost/retail/main/store",
        type: "POST",
        data: {
            /*barcode: barcode,
            productname: productname,
            sm: sm,
            spl: spl,
            quantity: quantity,
            price: price,
            discount: discount,
            amount: amount*/
            data:$('#inventory_form').serialize()
        },
        success: function (res) {
            alert(res);
        }

    });

这是我的控制器代码:

public function store(){



            /* echo ($_POST['barcode']);
             echo "<br/>";
             echo ($_POST['productname']);
             echo "<br/>";
             echo ($_POST['smsm']);
             echo "<br/>";
             echo ($_POST['spl']);
             echo "<br/>";
             echo ($_POST['quantity']);
             echo "<br/>";
             echo ($_POST['price']);
             echo "<br/>";
             echo ($_POST['discount']);
             echo "<br/>";
             echo ($_POST['amount']);
             echo "<br/>";*/


        //echo $this->input->post('barcode');






    if ($this->session->userdata('status')== 1) {
         for ($i = 0; $i < count($_POST['productname']); $i++) {

         $order_id = $this->session->userdata('id');
         $product_name = $this->input->post('productname')[$i];
         $barcode = $this->input->post('barcode')[$i];
         $spl= $this->input->post('spl')[$i];
         $sm= $this->input->post('sm')[$i];
         $quantity = $this->input->post('quantity')[$i];
         $price = $this->input->post('price')[$i];
         $discount =$this->input->post('discount')[$i];
         $amount = $this->input->post('amount')[$i];



        $data = array(

             'order_id'=> $order_id,
             'product_name'=>$product_name,
             'barcode'=> $barcode,
             'sm'=> $sm,
             'spl'=> $spl,
             'quantity'=>$quantity,
             'price'=>$price,
             'discount'=> $discount,
             'amount'=> $amount,



        );     

             $this->Sale_model->insert_sales_data($data);
             echo "Data inserted successfully!";    





         }
      }

      else 
      {
          redirect('login');
      }






    }

我正在使用Codeigniter。请告诉我我上面提到的功能如何?

为什么在ajax请求中获得单个值?

var barcode = $('#barcode  option:selected').val();
var productname = $('#brcode_product').val();
var sm = $('#sm  option:selected').val();
var spl = $('#spl').val();
var quantity = $('#quantity').val();
var price = $('#price').val();
var discount = $('#discount').val();
var amount = $('#amount').val();

为什么不只序列化表单然后传递该序列化数据呢?

$('#save_btn').on('click', function () { var formdataString = $("#inventory_form"); $.ajax({ url: "http://localhost/retail/main/store", type: "POST", data: formdataString.serialize(), success: function (res) { alert(res); } }); });

暂无
暂无

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

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