如何在数组中推送多个值

时间:2017-06-07 07:04:05

标签: jquery ajax codeigniter

我试图在数组中推送多个值,以便发送到php,但它只插入第一个值。如何使用jquery在数组中推送多个值,以便我可以将该数组发送到ajax并在php中处理它?<​​/ p>

这是我的HTML代码:

<form role="form" name="form_name" id="inventory_form" method="post" action="javascript:;">
                   <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>

这是我的代码: -

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

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





} 

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


            var barcode = [];
            var productname = [];
            var sm = [];
            var spl = [];
            var quantity = [];
            var price = [];
            var discount = [];
            var amount = [];

            $('.barcode  option:selected').each(function(){
                barcode.push = $('#barcode  option:selected').val();


            });
            $('.productname').each(function(){
                productname.push = $(this).val();


            });
            $('.sm  option:selected').each(function(){
                sm.push = $(this).val();


            });
            $('.spl').each(function(){
                spl.push = $(this).val();

            });
            $('.quantity').each(function(){
                quantity.push = $(this).val();

            });
            $('.price').each(function(){
                price.push = $(this).val();

            });
            $('.discount').each(function(){
                discount.push = $(this).val();

            });
            $('.amount').each(function(){
                amount.push = $(this).val();

            });

            console.log(barcode);
            console.log(productname);
            console.log(sm);
            console.log(spl);
            console.log(quantity);
            console.log(price);
            console.log(discount);
            console.log(amount);

            /*$.ajax({
                url: "http://localhost/retail/main/store",
                method: "POST",
                data: {
                    barcode: barcode,
                    productname: productname,
                    sm: sm,
                    spl: spl,
                    quantity: quantity,
                    price: price,
                    discount: discount,
                    amount: amount

                },
                success: function (data) {
                   alert(data);
                }

            });*/

            /*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;*/










    });

请不要考虑评论代码。

0 个答案:

没有答案