Ajax没有处理多个插入 - CI

时间:2016-12-01 00:49:37

标签: javascript php jquery ajax codeigniter

我希望使用codeigniter一次性保存多个数据,然后成功加载数据表上的数据,而无需刷新整个页面。如果我使用模态并插入单个值,但是当我尝试保存多个值时,我能够成功地执行此操作,但它无法正常工作。我点击按钮生成输入框。以下是我的代码:

CONTROLLER

<a name="mult_page">
<button class="btn btn-info" data-toggle="collapse" data-target="#add_multiple" style="margin-left: 20px;">Add Multiple Orders</button>
<div class="collapse" id="add_multiple" style="width: 95%; margin: 0 auto; margin-top: 10px;">
        <div class="row">
            <div class="col-md-12"> 
                <div class="panel panel-primary">
                    <div class="panel-heading">
                    </div>        
                    <div class="panel-body">
                        <form class="form_mult_ordrs form-inline"  method="post">

                            <div class="form-group">
                                <label for="m_date_added">Date</label>
                                <input type="date" name="m_date_added" required>
                            </div>

                            <div class="form-group">
                                <label for="m_branch_name" class="control-label">Branch Name</label>
                                <select name="m_branch_name" class="form-control">
                                    <option value="superdome">Superdome</option>';
                                    <option value="seaside">Sea Side</option>
                                    <option value="robinsons">Robinsons</option>
                                </select>               
                            </div>

                            <div class="btn btn-warning pull-right" onclick="add_new_row()">Add more</div>
                            <hr>
                            <div style="font-weight: bold;">Total Php <input type="text" id="total_result" placeholder="0.00" class="form-control"></div>
                            <br>

                            <table id="mult_ord_tbl" class="table table-striped table-bordered table-condensed">
                                <thead>
                                    <tr>
                                        <th class="ui-help-center">Item Name</th>
                                        <th class="ui-help-center">Quantity</th>
                                        <th class="ui-help-center">Amount</th>
                                        <th class="ui-help-center">Comment</th>
                                        <th class="ui-help-center">Delete</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td>
                                            <select name="item_name[]" class="form-control">
                                                <?php foreach($items as $item){
                                                    echo '<option value"='.$item->item_name.'">'.$item->item_name.'</option>';
                                                } ?>
                                            </select>
                                        </td>
                                        <td><input type="text" name="m_quantity[]" placeholder="Quantity"></td>
                                        <td><input type="text" name="m_amount[]" id='m_amount[]' placeholder="Amount" onblur="total_values()"></td>
                                        <td><input type="text" name="m_order_comment[]" placeholder="Commment"></td>
                                        <td>
                                            <button class="btn btn-danger" onclick="delete_row(this)"><i class="glyphicon glyphicon-remove"></i></button>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                                <tr>
                                    <td colspan="12">
                                        <button id="btn_mult_ordrs" class="btn btn-success" onclick="save_mult_ordrs()" value="">Submit All</button>
                                    </td>
                                </tr>
                        </form>                     
                    </div> <!-- end of panel body -->
                    <div class="panel-footer">
                        footer
                    </div>
                </div> <!-- end of panel primary -->
            </div> <!-- end of column 12 -->
        </div> <!-- end of row -->
</div> <!-- end of collapse -->

<script type="text/javascript">
$(document).ready(function(){
    $('#table_id').DataTable({
        "order":[[0,"desc"]]
    });
});

function save_mult_ordrs(){
    if(confirm("Are you done?")){
        var url="<?php echo site_url('sales/add_multiple_orders')?>";
        add_new_row();
        // $("#form_mult_ordrs").submit();

        $.ajax({
        url:url,
        type:"POST",
        data:$('#form_mult_ordrs').serialize(),
        datatype:"JSON",
        success:function(data)
        {
            alert('All data has been saved.');
            location.reload();
        },
        error:function(jqXHR, textStatus, errorThrown){
            alert('Error in saving.');
        }
    });
    }
}

function add_new_row(){

   var arrTables = document.getElementById('mult_ord_tbl');
   var oRows = arrTables.rows;
   var numRows = oRows.length;

   var newRow = document.getElementById('mult_ord_tbl').insertRow( numRows );
   var cell1=newRow.insertCell(0);
   var cell2=newRow.insertCell(1);
   var cell3=newRow.insertCell(2);
   var cell4=newRow.insertCell(3);
   var cell5=newRow.insertCell(4);

   cell1.innerHTML = "<tr><td><select name='m_item_name[]' class='form-control'>" + 
   <?php 
        foreach($items as $item){
            echo ('"<option value=\"'.$item->item_name.'\">'.$item->item_name.'</option>"+');
        }
    ?>
    + "</select></td>";
   cell2.innerHTML="<td height='5'><input type='text' name='m_quantity[]' placeholder='Quantity'></td>";
   cell3.innerHTML="<td height='5'><input type='text' name='m_amount[]' placeholder='Amount' onblur='total_values()'></td>"
   cell4.innerHTML="<td height='5'><input type='text' name='m_order_comment[]' placeholder='Comment'></td>";
   cell5.innerHTML="<td><button class='btn btn-danger' onclick='delete_row(this)''><i class='glyphicon glyphicon-remove'></i></button></td></tr>";
}
</script>

查看

public function insert_mult_orders($data){
    $this->db->insert_batch('piercapitan.sls_ordrs',$data);
    return $this->db->affected_rows();
}

MODEL

{{1}}

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

别介意的人。我发现了什么是错的。这是我的表格ID!我不知怎的把名字放在了班级而不是id上。

<form class="form_mult_ordrs form-inline"  method="post">

应该是:

<form id="form_mult_ordrs" class="form-inline"  method="post">