【问题标题】:save multiple fields with same name in database using ajax使用ajax在数据库中保存多个同名字段
【发布时间】:2017-06-06 08:43:34
【问题描述】:

我想保存当我尝试保存数据时动态生成的多行表,只有第一个字母保存在数据库中。当我尝试保存下一行时,它在控制台中显示 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。请告诉我我上面提到的功能如何实现?

【问题讨论】:

  • 您正在以数组$this-&gt;input-&gt;post('productname')[$i] 的形式接收值,但在发送时您并未以数组的形式发送。我认为以下答案应该对您有所帮助。你应该 serialize() 并传递给 ajax。
  • @NiranjanNRaju 当我使用 serialize() 函数将整个表单传递给 ajax 时,我在警报中得到空白响应..!!
  • 你能更新你的javascript代码吗?
  • 我已经编辑了我的代码,请检查..!!
  • 就这样做吧,data: $('#inventory_form').serialize()。无需再次作为数据传递。否则您必须更改 php 才能访问 $this-&gt;input-&gt;post() 中的 data 索引

标签: javascript php mysql ajax codeigniter


【解决方案1】:

为什么在 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();

为什么不直接序列化表单,然后传递序列化数据?

【讨论】:

  • 好吧,我想一一传递并保存这些数据,我知道我在某处缺少 id,但我不知道如何将增加的 id 传递给 ajax 函数。请告诉我如何我可以获取每个输入的值并选择标签以及 id 并将其传递给 ajax 函数吗?
  • @PranaySute,如果您将整个表单传递到后端,您的后端/php 代码将具有值数组。您需要检查的事项: 1. 确保表单的值在后端传递(执行 print_r 或 var_dump 以查看您的请求传递的数据)。 2. 我可以看到的另一个问题是您的 id 是数组形式(例如 myid[1]),而您在 js 中通过确切的 id 访问它们(例如 $('#myid').val())。这可能是您收到单个字母的原因。尝试在执行 ajax 请求之前检查您传递的值。尝试执行 console.log 并检查控制台结果
【解决方案2】:
$('#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); } }); });

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
猜你喜欢
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 2023-04-10
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多