I have a form like below, and it is dynamically adding and removing fields. What I want to achieve is to insert all data from my form while it should not matter how many rows it has already present.
<tr>
<td><input name="model[]" type="text" class="input-large" id="A1" value="" /></td>
<td><input name="color[]" type="text" class="input-small" id="Color1" value="" /></td>
<td><input name="price[]" type="text" class="input-small" id="B1" value="" data-format="0,0[.]00" /></td>
<td><input name="quantity[]" type="text" class="input-small" id="C1" value="" data-format="0" /></td>
<td><input name="addon[]" type="text" class="input-small" id="D1" value="" data-format="0,0[.]00" /></td>
<td><input name="discount[]" type="text" class="input-small" id="E1" value="" data-format="0,0[.]00" /></td>
<td><input name="total[]" type="text" class="input-small" id="F1" value="" data-formula="($B1*$C1)+($D1-$E1)" readonly /></td>
<td><input type="button" value="remove" class="btn-remove btn btn-mini btn-danger" /></td>
</tr>
javascript:
<script type="text/javascript">
var currentRow = 1;
$(document).ready(function(){
$('#calx').calx();
$('#add_item').click(function(){
var $calx = $('#calx');
currentRow++;
$calx.append('<tr>\
<td><input type="text" name="model[]" class="input-large" id="A'+currentRow+'" value="" /></td>\
<td><input type="text" name="color[]" class="input-small" id="Color1'+currentRow+'" value="" /></td>\
<td><input type="text" name="price[]" class="input-small" id="B'+currentRow+'" value="" data-format="0,0[.]00" /></td>\
<td><input type="text" name="quantity[]" class="input-small" id="C'+currentRow+'" value="" data-format="0" /></td>\
<td><input type="text" name="addon[]" class="input-small" id="D'+currentRow+'" value="" data-format="0,0[.]00" /></td>\
<td><input type="text" name="discount[]" class="input-small" id="E'+currentRow+'" value="" data-format="0,0[.]00" /></td>\
<td><input type="text" name="total[]" class="input-small" id="F'+currentRow+'" value="" data-format="" data-formula="($B'+currentRow+'*$C'+currentRow+'+$D'+currentRow+'-$E'+currentRow+')" /></td>\
<td><input type="button" value="remove" class="btn-remove btn btn-mini btn-danger" /></td>\
</tr>');
//update total formula
$('#G1').attr('data-formula','SUM($F1,$F'+currentRow+')');
$calx.calx('refresh');
});
$('#calx').on('click', '.btn-remove', function(){
$(this).parent().parent().remove();
$('#calx').calx('refresh');
});
});
</script>
I have a controller like:
public function add_transactions(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('model[]', 'Item Name or Model', 'trim|required|xss_clean');
$this->form_validation->set_rules('color[]', 'Color', 'trim|required|xss_clean');
$this->form_validation->set_rules('price[]', 'Item Price', 'trim|required|xss_clean');
$this->form_validation->set_rules('quantity[]', 'Quantity', 'trim|required|xss_clean');
$this->form_validation->set_rules('addon[]', 'Add-on', 'trim|required|xss_clean');
$this->form_validation->set_rules('discount[]', 'Discount', 'trim|required|xss_clean');
$this->form_validation->set_rules('total[]', 'Unit cost or Amount', 'trim|required|xss_clean');
if($this->form_validation->run() == FALSE){
$code = $this->input->post('code');
$this->load->model('purchase_order');
$this->load->model('supplier');
$data['result'] = $this->purchase_order->getPurchaseOrderByID($code);
$data['results'] = $this->supplier->SupplierList();
$data['main_content'] = 'purchase_order_pricing.php';
$this->load->view('dashboard',$data);
} else {
$this->load->model('purchase_order');
$result = $this->purchase_order->AddPurchaseOrder($data);
if(!$result['error'])
{
$this->session->set_flashdata('flashSuccess', 'Purchase Order transaction has been updated.');
redirect('home/purchase_order_view', 'refresh');
$data['main_content'] = 'purchase_order_view.php';
$this->load->view('dashboard',$data);
}
else
{
$this->session->set_flashdata('flashSuccess', 'The server is busy please try again later.');
redirect('home/purchase_order_view', 'refresh');
$data['main_content'] = 'purchase_order_view.php';
$this->load->view('dashboard',$data);
}
}
}
And a model like:
public function AddPurchaseOrder($data){
$data =array();
for($i=0; $i<sizeof($data); $i++) {
$data[$i] = array(
'model' => $model[$i],
'color' => $color[$i],
'price' => $price[$i],
'quantity' => $quantity[$i],
'addon'=>$addon[$i],
'discount'=>$discount[$i],
'total'=>$total[$i],
'code'=>$code[$i]
);
}
$this->db->insert('purchase_order_info',$data);
}