1

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.

Ibanez1408
  • 4,550
  • 10
  • 59
  • 110
  • Is there some error which is shown ? Try to change the ini config to show all errors. – Vasil Rashkov Dec 13 '16 at 08:59
  • Can you tell me how to that please. "Show all errors". – Ibanez1408 Dec 13 '16 at 09:07
  • I am not sure how to do that in codeigniter, but here is a tip http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Vasil Rashkov Dec 13 '16 at 09:09
  • Besides what @VasilShaddix said, also check your Network tab in the browser console, see what's what there too. – Andrei Dec 13 '16 at 09:10
  • I'll do that... Meanwhile, I was able to see the error. I forgot to load the model in the construct of the controller. It is difficult for me to debug because the only error message I get is failed to load response or Internal error 500. With your suggestion, it would be great if I would be able to get the real error. Thank you for your time. – Ibanez1408 Dec 13 '16 at 09:13
  • in your entry point (for ex index.php) you can add `define('ENVIRONMENT', 'development');` to display errors on runtime, for easy debug. – Vasil Rashkov Dec 13 '16 at 09:16

0 Answers0