I have an form in which I need to add dynamic field. the code to form of codeigniter is below
<?php echo form_open('admin/sales/create' , array('class' => 'form-horizontal form-groups-bordered validate','target'=>'_top'));?>
<div class="form-group">
<div class="col-sm-2">
<label>Date</label>
<input type="text" class="datepicker form-control" name="date" data-validation="date" data-validation-format="mm/dd/yyyy"/>
</div>
<div class="col-sm-2">
<label>Invoice No.</label>
<input type="text" class="form-control" name="invoice" data-validation="required" data-validation-error-msg="Required Field"/>
</div>
<div class="col-sm-3">
<label>Customer Name</label>
<input type="text" class="form-control" name="cname" value="Cash" data-validation="required" data-validation-error-msg="Required Field"/>
</div>
<div class="col-sm-2">
<label>Payment Received</label>
<input type="text" class="form-control" name="pay" data-validation="required" data-validation-error-msg="Required Field"/>
</div>
</div>
<div id="row0" class="form-group rows">
<div class="col-sm-4">
<label>Product Name</label>
<select name="pname[]" class="form-control">
<?php
$this->db->order_by('name','asc');
$students = $this->db->get('product')->result_array();
foreach($students as $row):
?>
<?php $qnty=$this->crud_model->count_products($row['prod_id']);?>
<option value="<?php echo $row['prod_id'];?>" <?php if($qnty==0) echo 'disabled'; ?> >
<?php echo ucwords($row['name']);?>
(Available Qnty = <?php echo $this->crud_model->count_products($row['prod_id']);?>)
(Average Price = <?php echo $this->crud_model->avg_price($row['prod_id']);?>)
</option>
<?php
endforeach;
?>
</select>
</div>
<div class="col-sm-4">
<label>Quantity (Should be less than Available Qnty)</label>
<input type="text" class="form-control" name="qnty[]" data-validation="required" data-validation-error-msg="Required Field" />
</div>
<div class="col-sm-2">
<label>Price / Unit</label>
<input type="text" class="form-control" name="price[]" data-validation="required" data-validation-error-msg="Required Field" />
</div>
<div class="col-sm-2">
<label>Subtotal</label>
<input type="text" class="form-control" name="tot[]" data-validation="required" data-validation-error-msg="Required Field" />
</div>
</div>
<a href="javascript:" onclick="hello();">Add Row</a>
<div class="form-group">
<div class="col-sm-6">
<button type="submit" class="btn btn-primary">Add Invoice</button>
</div>
</div>
</form>
when click on Add row, It will clone the div with having row0. Now I need to calculate the field of these rows. My formula is
Qnty Price Subtotal
5 10 50
7 12 84
Grand total 134 (total of all subtotal)
I used below function to clone the row
function hello()
{
$("#row0").clone().insertAfter("div.rows:last");
var list = document.getElementsByClassName("rows");
for (var i = 1; i < list.length; i++) {
list[i].setAttribute("id", "row" + i);
}
}
Thanks for any suggestion using ajax or any method.