weixin_33674976 2016-12-13 08:56 采纳率: 0%
浏览 18

Ajax插入无法正常工作

I am trying to insert multiple data at the same time. In my console I am able to get the posts but somehow it is not inserting in my database. Below are my codes.

VIEW

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

Can you please tell me where I am getting this wrong? Thank you.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 C# datagridview 栏位进度
    • ¥15 vue3页面el-table页面数据过多
    • ¥100 vue3中融入gRPC-web
    • ¥15 kali环境运行volatility分析android内存文件,缺profile
    • ¥15 写uniapp时遇到的问题
    • ¥15 vs 2008 安装遇到问题
    • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
    • ¥15 找一个网络防御专家,外包的
    • ¥100 能不能让两张不同的图片md5值一样,(有尝)
    • ¥15 informer代码训练自己的数据集,改参数怎么改