user7047368 user7047368 - 11 days ago 5
MySQL Question

getting total value after entering the values in the form and inserting into database using codeigniter

If i select Local Sales from dorpdown and enter DEF, GHI values then the sum of DEF,GHI should be displayed in total value or if i select Inter State,Stock Transfers from dropdown then if we enter ABC value that value should be displayed in total value or else if we select JOB WORK,EXEMPTED SALES from dropdown then the total value should be displayed as zero. The total value which ever we are getting that should be inserted into database.

Controller:

function addinvoice()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('user','User');
$this->form_validation->set_rules('freight_charges');
$this->form_validation->set_rules('abc');
$this->form_validation->set_rules('def');
$this->form_validation->set_rules('ghi');
$this->form_validation->set_rules('total');
if($this->form_validation->run()== FALSE)
{
$data['mainpage']='invoice';
$data['mode']='add';
$this->load->view('templates/template',$data);
}
else
{
$this -> invoice_model -> insert();
$this->flash->success('<h2> Details added Successfully!</h2>');
redirect('invoice');
}
}


Model:

function insert()
{
$data['total']=0;
$data['user'] = $this->input->post('user');
$data['ghi'] = ($this->input->post('ghi'))?$this->input->post('ghi'):0;
$data['abc'] = ($this->input->post('abc'))?$this->input->post('abc'):0;
$data['def'] = ($this->input->post('def'))?$this->input->post('def'):0;
$data['total'] = $data['ghi'] + $data['abc'] + $data['def'];
$data['freight_charges'] = $this->input->post('freight_charges');
$this->db->insert('invoice',$data);
}


View:

<script>
function showRequiredOption(cval)
{
if((cval=='interstate') || (cval == "stocktransfers"))
{
$('#ghi').hide();
$('#def').hide();
$('#abc').show();
}
else if ((cval=='exemptedsales') || (cval=="zeroratedsales") ||(cval=="jobwork"))
{
$('#ghi').hide();
$('#def').hide();
$('#abc').hide();

}
else
{
$('#abc').hide();
$('#ghi').show();
$('#def').show();

}
}
</script>
<div class="col-md-9 col-md-offset-2">
<div id="legend">
<legend class="">Profile Information</legend>
</div>
<form role="form" action="<?php echo site_url();?>invoice/addinvoice" method="post" class="form-horizontal" id="location" method="post" accept-charset="utf-8">
<div class="form-group">
<label class="control-label col-sm-2 " for="user">User</label>
<div class="col-sm-4 col-sm-offset-1">
<select id="user" name="user" onchange="showRequiredOption(this.value)">
<option value="employee">Local Sales</option>
<option value="interstate">Inter state</option>
<option value="stocktransfers">Stock transfers</option>
<option value="exemptedsales">Exempted Sales</option>
<option value="zeroratedcompany">Zero Rated Sales</option>
<option value="jobwork">Job Work</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="freight_charges">Freight Charges</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="freight_charges" name="freight_charges" value="<?php echo set_value('freight_charges');?>" />
</div>
</div>

<div class="form-group" id="abc" style="display:none;">
<label class="control-label col-sm-2 " for="abc">ABC</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="abc" name="abc" value="<?php echo set_value('abc');?>"/ >
</div>
</div>
<div class="form-group" id="def">
<label class="control-label col-sm-2 " for="def">DEF </label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="def" name="def" value="<?php echo set_value('def');?>"/ >
</div>
</div>
<div class="form-group" id="ghi">
<label class="control-label col-sm-2 " for="ghi">GHI</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="ghi" name="ghi" value="<?php echo set_value('ghi');?>"/ >
</div>
</div>
<div class="form-group" id="cgst">
<label class="control-label col-sm-2 " for="total">Total</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" name="total" >
</div>
</div>
<button id="submit" type="submit" class="btn" name="submit">Submit</button>
</form>
</div>


Whatever values i have selected from dropdown only those values to be inserted into database and the rest of the values should be inserted as zero in the database.

Actually i am not getting how to do these can anyone check this.Thanks in Advance.

Answer

Displaying total amount on enter the details.

<script>
function showRequiredOption(cval)
{
if((cval=='interstate') || (cval == "stocktransfers"))
{
$('#ghi').hide();
$('#def').hide();
$('#abc').show();
}
else if ((cval=='exemptedsales') || (cval=="zeroratedsales") ||   (cval=="jobwork"))
{
$('#ghi').hide();
$('#def').hide();
$('#abc').hide();
}
else
{
$('#abc').hide();
$('#ghi').show();
$('#def').show();
}
}
</script>
<script>
$(document).ready(function(){

//iterate through each textboxes and add keyup
//handler to trigger sum event
$(".txt").each(function() {

  $(this).keyup(function(){
    calculateSum();
  });
});

});

function calculateSum() {
var sum = 0;
//iterate through each textboxes and add the values
$(".txt").each(function() {
  //add only if the value is number
  if(!isNaN(this.value) && this.value.length!=0) {
    sum += parseFloat(this.value);
  }
  });
//.toFixed() method will roundoff the final sum to 2 decimal places
  $("#sum").html(sum.toFixed(2));
  }
  </script>
  <div class="col-md-9 col-md-offset-2">
  <div id="legend">
  <legend class="">Profile Information</legend>
  </div>  
  <form role="form" action="<?php echo site_url();?>invoice/addinvoice" method="post" class="form-horizontal" id="location" method="post" accept-charset="utf-8">     
  <div class="form-group">
  <label class="control-label col-sm-2 " for="user">User</label> 
  <div class="col-sm-4 col-sm-offset-1">
    <select id="user" name="user" onchange="showRequiredOption(this.value)">
        <option value="employee">Local Sales</option>
        <option value="interstate">Inter state</option>
        <option value="stocktransfers">Stock transfers</option>
        <option value="exemptedsales">Exempted Sales</option>
        <option value="zeroratedcompany">Zero Rated Sales</option>
        <option value="jobwork">Job Work</option>
    </select>
   </div>
   </div>      
  <div class="form-group">
  <label class="control-label col-sm-2 " for="freight_charges">Freight Charges</label>
  <div class="col-sm-4 col-sm-offset-1">
  <input type="text" class="form-control txt" id="freight_charges" name="freight_charges" value="<?php echo set_value('freight_charges');?>"  />
  </div>
  </div>
  <div class="form-group" id="abc" style="display:none;">
  <label class="control-label col-sm-2 " for="abc">ABC</label>
  <div class="col-sm-4 col-sm-offset-1">
  <input type="text" class="form-control txt" id="abc" name="abc" value="<?php echo set_value('abc');?>"/ >
  </div>
  </div>  
  <div class="form-group" id="def">
  <label class="control-label col-sm-2 " for="def">DEF </label>
  <div class="col-sm-4 col-sm-offset-1">
  <input type="text" class="form-control txt" id="def" name="def" value="<?php echo set_value('def');?>"/ >
  </div>
  </div>  
  <div class="form-group" id="ghi">
  <label class="control-label col-sm-2 " for="ghi">GHI</label>
  <div class="col-sm-4 col-sm-offset-1">
  <input type="text" class="form-control txt" id="ghi" name="ghi" value="<?php echo set_value('ghi');?>"/  >
  </div>
  </div> 
  <div class="form-group" id="summation">
  <label class="control-label col-sm-2 " for="total">Total</label>
  <div class="col-sm-4 col-sm-offset-1">
  <span id="sum" class="form-control" type="text" name="total">0</span>
  </div>
  </div>      
  <button id="submit" type="submit" class="btn" name="submit">Submit</button>
  </form>
  </div>