2016-12-01 85 views
0

我想使用codeigniter一次性保存多個數據,然後在成功的情況下在刷新整個頁面時加載數據表中的數據。如果我使用模態並插入單個值,但是當我嘗試保存多個值時無法正常工作,則可以成功完成此操作。我點擊按鈕生成我的輸入框。下面是我的代碼:Ajax無法處理多個插入-CI

控制器

public function add_multiple_orders(){ 
     $dataset=[]; 
     $item_name=$this->input->post('m_item_name'); 
     $quantity=$this->input->post('m_quantity'); 
     $amount=$this->input->post('m_amount'); 
     $order_comment=$this->input->post('m_order_comment'); 
     $branch_name=$this->input->post('m_branch_name'); 
     $date_added=$this->input->post('m_date_added'); 

     for($i=0;$i<sizeof($item_name);$i++){ 
      $dataset[$i]=array(
       'date_added'=>$date_added, 
       'item_name'=>$item_name[$i], 
       'quantity'=>$quantity[$i], 
       'amount'=>$amount[$i], 
       'ordered_by'=>$this->session->userdata['username'], 
       'order_status'=>'ordered', 
       'order_comment'=>$order_comment[$i], 
       'branch_name'=>$branch_name 
       ); 
     } 
     $result=$this->sales_model->insert_mult_orders($dataset); 
    } 

VIEW

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

你的幫助是極大的讚賞。

回答

0

沒關係傢伙。我發現什麼是錯的。這是我的表單ID!我以某種方式將名稱放在課程上,而不是放在ID上。

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

它應該是:

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

好東西。任何你需要命名你的id form_mult_ordrs而不是form_multiple_orders或類似的原因......爲了某種形式的簡潔而使用缺少的字母可能會導致頭痛。 – TimBrownlaw

+1

我認爲幾個字符可以節省時間。但下次創建這些名稱時我會記住這一點。 – Ibanez1408