Ajax插入不起作用

时间:2016-12-13 08:56:23

标签: javascript php ajax codeigniter-3

我正在尝试同时插入多个数据。在我的控制台中,我能够获得帖子,但不知何故它没有插入我的数据库。以下是我的代码。

查看

<h2 id="sec0"></h2>
<div class="row">
    <div class="col-md-12" style="margin: 0 auto; margin-top: 10px;">
        <div class="panel panel-primary">
            <div class="panel-heading"><h3>New Orders</h3></div>
            <div class="panel-body">
                <form class="form-inline" role="form" id="form_add_orders" method="post">
                    <div class="form-group">
                        <label for="date_added">Date</label>
                        <input type="date" name="date_added">

                        <label class="control-label">Branch Name</label>
                            <select name="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" id="btn_add_more">Add More</div>
                    <hr>
                    <div style="font-weight: bold;">Total Php <input type="text" id="order_total" placeholder="0.00" class="form-control" readonly></div>
                    <br>
                    <table class="table table-striped table-bordered table-condensed" id="tbl_new_orders">
                        <colgroup>
                            <col span="1" style="width: 40%;">
                            <col span="1" style="width: 15%;">
                            <col span="1" style="width: 15%;">
                            <col span="1" style="width: 5%;">
                        </colgroup>
                        <thead>
                            <tr>
                                <th>Item Name</th>
                                <th>Quantity</th>
                                <th>Amount</th>
                                <th>Delete</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>
                                    <input type="text" name="item_name[]" id="item_name" placeholder="Item Name" class="form-control input-sm" style="width: 100%;" required>
                                </td>
                                <td>
                                    <input type="text" name="quantity[]" placeholder="Amount" class="form-control input-sm" style="width: 100%;" required>
                                </td>
                                <td>
                                    <input type="number" name="amount[]" placeholder="Amount" style="width: 100%" class="form-control input-sm" onblur="total_order_amount()" required>
                                </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_save_orders" class="btn btn-success pull-right" onclick="save_orders()">Submit Orders</button>
                        </td>
                    </tr>
                </form>
            </div><!-- end of panel body -->
        </div><!-- end of panel -->
    </div><!-- end of col-md-12 -->
</div> <!-- end of sec 0 -->

<script type="text/javascript">

function save_orders(){
        if(confirm("Are you done?")){
            var url="<?php echo site_url('store_cashier/submit_orders')?>";
            $.ajax({
                url:url,
                type:"POST",
                data:$('#form_add_orders').serialize(),
                datatype:"JSON",
                success:function(data)
                {
                    //location.reload();
                    console.log(data);
                },
                error:function(jqXHR, textStatus, errorThrown){ 
                    alert('Error in saving records. '+errorThrown);
                }
            });
        }
    }
</script>

MODEL

Class Order extends CI_Model{

    var $table='orders';

    public function __construct(){
        parent::__construct();
    }

    public function save_orders($data){
        $this->db->insert_batch('piercapitan.orders', $data);
    }
}

CONTROLLER

public function submit_orders(){
        $item_name=$this->security->xss_clean($this->input->post('item_name[]'));
        $quantity=$this->security->xss_clean($this->input->post('quantity[]'));
        $amount=$this->input->post('amount[]');

        for($i=0;$i<sizeof($item_name);$i++){
            $dataset[$i]=array(
                'item_name'=>$item_name[$i],
                'quantity'=>$quantity[$i],
                'amount'=>$amount[$i],
                'ordered_by'=>$this->session->userdata['username'],
                'ordered_date'=>$this->input->post('date_added'),
                'branch_name'=>$this->input->post('branch_name'),
                'status'=>'ordered'
            );
        }
        $this->order->save_orders($dataset);
        echo json_encode(array('status'=>TRUE));
    }

你能告诉我我错在哪里吗?谢谢。

0 个答案:

没有答案