如何将每个输入值与特定id一起发送到ajax函数?

时间:2017-06-06 11:10:14

标签: jquery codeigniter

我遇到了一个问题,我对此非常感激不尽。我没有得到如何发送特定的输入ID,以便我可以将其发送到ajax然后保存到数据库。我想将所有数据发送到ajax,在我的表中,输入字段是动态生成的。我们来看看下面的图片。 Invoice Image

我的jquery函数:

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



        var barcode = $('#barcode[' + product_barcode_counter + '] option:selected').val();
        var productname = $('#brcode_product[' + product_name_counter + ']').val();
        var quantity = $('#barcode[' + product_quantity_counter + ']').val();
        var sm = $('#sm[' + sm_counter + ']').val();
        var spl = $('#spl[' + spl_counter + ']').val();
        var price = $('#price[' + product_price_counter + ']').val();
        var discount = $('#discount[' + product_discount_counter + ']').val();
        var amount = $('#amount[' + product_amount_counter + ']').val();

        $('#save_btn').on('click', function (id) {

            $.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
                },
                success: function (res) {
                    alert(res);
                }

            });

        })







    //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();

    //Send ajax request to the function on pressing the save button






}

我的控制器:

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($this->input->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');
      }






    }

当你点击蓝色按钮时,首先在下面添加相同的行,所以我只想问我如何将每行的值发送到ajax函数,以便我可以相应地保存我的数据库中的记录将点击保存按钮(绿色)。我没有得到如何使用jquery和php在ajax函数的帮助下实现这一点。!

1 个答案:

答案 0 :(得分:0)

将以下代码用于您的目的

<form role="form" name="form_name" method="post" action="javascript:;">
  <!--Form Elements-->
</form>

使用FormData对象传递AJAX请求

var data = new FormData($('form')[0]);
$.post({
        method: "POST",
        url: "http://localhost/retail/main/store",
        data: data,
        cache: false,
        contentType: false,
        processData: false,
}).done(function( response ) {

}).error(function(response){

});