2017-05-27 8 views
1

誰でも動的入力からデータベースにデータを挿入します。codeigniter

私はCodeIgniterのでは初心者だけど、私はデータベースへのダイナミック入力からデータを挿入しようと、私はあまり検索さが、私は今のところ

私はどのようにわからないので、私のモデルは今まで空で何も得ませんでしたこれは、このjQueryの

<script> 
$(document).ready(function() { 
    var titleValidators = { 
      row: '.col-xs-4', // The title is placed inside a <div class="col-xs-4"> element 
      validators: { 
       notEmpty: { 
        message: 'Product is required' 
       } 
      } 
     }, 
     isbnValidators = { 
      row: '.col-xs-4', 
      validators: { 
       notEmpty: { 
        message: 'Quantity is required' 
       } 
      } 
     }, 
     priceValidators = { 
      row: '.col-xs-2', 
      validators: { 
       notEmpty: { 
        message: 'Invoice Number is required' 
       }, 
       numeric: { 
        message: 'The invoice number must be a numeric number' 
       } 
      } 
     }, 
     bookIndex = 0; 

    $('#bookForm') 
     .formValidation({ 
      framework: 'bootstrap', 
      icon: { 
       valid: 'glyphicon glyphicon-ok', 
       invalid: 'glyphicon glyphicon-remove', 
       validating: 'glyphicon glyphicon-refresh' 
      }, 
      fields: { 
       'product[0].title': titleValidators, 
       'product[0].qty': isbnValidators, 
       'product[0].price': priceValidators 
      } 
     }) 

     // Add button click handler 
     .on('click', '.addButton', function() { 
      bookIndex++; 
      var $template = $('#bookTemplate'), 
       $clone = $template 
        .clone() 
        .removeClass('hide') 
        .removeAttr('id') 
        .attr('data-book-index', bookIndex) 
        .insertBefore($template); 

      // Update the name attributes 
      $clone 
       .find('[name="title"]').attr('name', 'product[' + bookIndex + '].title').end() 
       .find('[name="qty"]').attr('name', 'product[' + bookIndex + '].isbn').end() 
       .find('[name="price"]').attr('name', 'product[' + bookIndex + '].price').end(); 

      // Add new fields 
      // Note that we also pass the validator rules for new field as the third parameter 
      $('#bookForm') 
       .formValidation('addField', 'product[' + bookIndex + '].title', titleValidators) 
       .formValidation('addField', 'product[' + bookIndex + '].qty', isbnValidators) 
       .formValidation('addField', 'product[' + bookIndex + '].price', priceValidators); 
     }) 

     // Remove button click handler 
     .on('click', '.removeButton', function() { 
      var $row = $(this).parents('.form-group'), 
       index = $row.attr('data-book-index'); 

      // Remove fields 
      $('#bookForm') 
       .formValidation('removeField', $row.find('[name="product[' + index + '].title"]')) 
       .formValidation('removeField', $row.find('[name="product[' + index + '].qty"]')) 
       .formValidation('removeField', $row.find('[name="product[' + index + '].price"]')); 

      // Remove element containing the fields 
      $row.remove(); 
     }); 
}); 
私の見解

<form id="bookForm" method="post" class="form-horizontal"action="order/insert"> 
      <div class="form-group"> 
       <div class="col-xs-3"> 
       <label for="customer_name">Customer : 
        <br> 
       <select class="selectpicker" data-show-subtext="true" data-live-search="true" name="customer_name" id="customer_name"> 
        <?php foreach ($customerdata as $c): 
        echo "<option value ='$c->c_id'>" . $c->c_name . "</option>"; 
        endforeach; 
        ?> 
       </select> 
       </label> 
       </div> 
       <div class="col-xs-3"> 
        <label for="invoice_number">Invoice Number : 
       <input type="text" class="form-control" name="invoice_number" id="invoice_number" placeholder="Invoice Number"/> 
        </label> 
       </div> 
       <div class="col-xs-3"> 
        <label for="branch">Branch : 
         <input type="text" class="form-control" name="branch" id="branch" placeholder="Branch"/> 
        </label> 
       </div> 
       <div class="col-xs-3"> 
        <label for="payment_term">Payment Term : 
         <br> 
         <select class="selectpicker" data-show-subtext="true" data-live-search="true" name="payment_term" id="payment_term"> 
          <option id-="cash">Cash</option> 
          <option id-="bank">Bank</option> 
          <option id-="other">Other</option> 
         </select> 
        </label> 
       </div> 
       <br> 

       <br><br><br><hr> 
       <label class="col-xs-1 control-label">Product</label> 

       <div class="col-xs-4"> 
        <select class="selectpicker" data-show-subtext="true" data-live-search="true" name="product[0].title"> 
         <?php 
         foreach($order as $row): 
          echo"<option value ='$row->p_id'>".$row->p_name. "</option>"; 
         endforeach; 
         ?> 
        </select> 
       </div> 
       <div class="col-xs-4"> 
        <input type="text" class="form-control" name="product[0].qty" placeholder="Quantity" /> 
       </div> 
       <div class="col-xs-2"> 
        <input type="text" class="form-control" name="product[0].price" placeholder="Price" /> 
       </div> 
       <div class="col-xs-1"> 
        <button type="button" class="btn btn-default addButton"><i class="fa fa-plus"></i></button> 
       </div> 
      </div> 

      <!-- The template for adding new field --> 
      <div class="form-group hide" id="bookTemplate"> 
       <div class="col-xs-4 col-xs-offset-1"> 
        <select class="selectpicker" data-show-subtext="true" data-live-search="true" name="title"> 
         <?php 
         foreach($order as $row): 
          echo"<option value ='$row->p_id'>".$row->p_name. "</option>"; 
         endforeach; 
         ?> 
        </select> 
       </div> 
       <div class="col-xs-4"> 
        <input type="text" class="form-control" name="qty" placeholder="Quantity" /> 
       </div> 
       <div class="col-xs-2"> 
        <input type="text" class="form-control" name="price" placeholder="Price" /> 
       </div> 
       <div class="col-xs-1"> 
        <button type="button" class="btn btn-default removeButton"><i class="fa fa-minus"></i></button> 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-xs-5 col-xs-offset-1"> 
        <button type="submit" class="btn btn-default">Submit</button> 
       </div> 
      </div> 
     </form> 

あるダイナミック入力

からデータを挿入

この私のコントローラ

function OrderNow(){ 

$this->load->model('Out_m'); 
$this->Out_m->outs(); 
$this->session->set_flashdata('done','Your Order Submitted Successfully'); 
redirect('Order', 'refresh'); 

} 

答えて

0

はちょうどあなたのフォーム入力を検証しているしかし、あなたは必要な参照してください。

$this->load->library('form_validation'); 
$this->form_validation->set_rules('branch', 'Branch', 'required|trim'); 

は、フォームの検証が失敗しないことを確認してください。その後

if ($this->form_validation->run() === FALSE) { 
    // Throw some error or whatever 
} else { 
    $form_data = array('branch'=>set_value('branch'); 
} 

以上のモデルにチャック:あなたはそこにそれを得れば

$this->Out_m->handling_function($form_data); 

、あなたがそれでやりたいことができます。