How To Calculate Two Textfield Values And Display Total In Another Textfield.

Hii all,

   I have one form where i want to calculate 2 textfields values and display total in another textfiled.

Here is my form:

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'mstpayroll-form','enableAjaxValidation'=&gt;false,

)); ?>

<div class="row">

  &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'FixSalary'); ?&gt;


  &lt;?php echo &#036;form-&gt;textField(&#036;model,'FixSalary'); ?&gt;


  &lt;?php echo &#036;form-&gt;error(&#036;model,'FixSalary'); ?&gt;

</div>

<div class="row">

  &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'Workingdays'); ?&gt;


  &lt;?php echo &#036;form-&gt;textField(&#036;model,'Workingdays'); ?&gt;


  &lt;?php echo &#036;form-&gt;error(&#036;model,'Workingdays'); ?&gt;

</div>

<div class="row">

   &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'Total'); ?&gt;


&lt;?php echo &#036;form-&gt;textField(&#036;model,'Total'); ?&gt;


&lt;?php echo &#036;form-&gt;error(&#036;model,'Total'); ?&gt;

</div>

i want to calculate fixsalary * workingdays and show the total to total textfield. Plz guide me thorugh this.

Dear Pramod,

This can be done by jQuery or Javascript:

On update of a textfield make a call to a function which calculates the total.




$(document).ready(function(){

  salaryId = 'mstpayroll_FixSalary';   /* Please check if this is correct */

  wdaysId = 'mstpayroll_Workingdays';

  

  $('#'+ salary +', #'+wdaysId).on(function(){

    var salary = $('#'+salaryId).val();

    var days = $('#'+wdaysId).val();

    if(salary == null) salary = 0;

    if(days == null) days = 0;


    var total = salary * days;

    $('#mstpayroll_Total').val(total);

  });

});

I have not tested the code, but I think this might be a suggestion for you.

Good luck!

Hallo. I´m Marcos from Brasil. I would like to know how to calculate qty x unit price = total. I found something like that but just it works in the item one. In the next ones, do not work. What i found is that.
Could someone help me how to do that in all items ?
Thank you so much.

Yii::app()->clientScript->registerScript(‘totalitem’, "
$(’#nm_unitario’).focusout(function(){
var quantidade_kg = $(’#ds_qtde’).val();
var preco_venda = $(’#nm_unitario’).val();
var calculo = parseFloat(quantidade_kg*preco_venda);
var total_item = calculo.toFixed(2);
$(’#nm_total’).val(total_item);
});"

AND ITS WORKING ON IT BELLOW BUT JUST IN THE FIRST ITEM.

<?php echo CHtml::activeHiddenField($model, "[{$index}]id_membro_cargo"); echo CHtml::activeDropDownList( $model,"[{$index}]id_cargo", $cargos, array('class' => 'form-control input-sm', 'prompt' => 'Selecione...')); ?> <?php echo CHtml::error( $model,"[{$index}]id_cargo"); ?>
	 <div class="col-lg-1 col-md-1 col-sm-1">
        <?php 
            echo CHtml::activeTextField( $model, "[{$index}]ds_un", array('size'=>20,'maxlength'=>3,'class' => 'form-control input-sm',  ));
        ?>
        <?php echo CHtml::error( $model,"[{$index}]ds_un"); ?>
    </div>
	
	<div class="col-lg-1 col-md-1 col-sm-1">
        <?php 
            echo CHtml::activeTextField( $model, "[{$index}]ds_qtde", array('id' => 'ds_qtde','size'=>20,'maxlength'=>8,'class' => 'form-control input-sm',  ));
        ?>
        <?php echo CHtml::error( $model,"[{$index}]ds_qtde"); ?>
    </div>
	
	<div class="col-lg-1 col-md-1 col-sm-1">
        <?php 
            echo CHtml::activeTextField( $model, "[{$index}]nm_unitario", array('id' => 'nm_unitario','size'=>20,'maxlength'=>8,'class' => 'form-control input-sm',  ));
                                                                        
	  ?>
        <?php echo CHtml::error( $model,"[{$index}]nm_unitario"); ?>
    </div>
	
	<div class="col-lg-1 col-md-1 col-sm-1">
        <?php 
            echo CHtml::activeTextField( $model, "[{$index}]nm_total",  array('id' => 'nm_total', 'size'=>20,'maxlength'=>8,'class' => 'form-control input-sm',  ));
 
	?>
        <?php echo CHtml::error( $model,"[{$index}]nm_total"); ?>
		 
    </div>
 
    <div class="col-lg-2 col-md-2 col-sm-2">
        <?php
            $properties = array(
                'class' => 'btn btn-xs btn-danger',
                'style' => 'margin-top: 3px;'
            );

            if ( !empty( $model->id_membro_cargo ) ) {
                $properties[ 'ajax' ] = array(
                    'method'    => 'post',    
                    'url'       => array( 'RemoverCargo' ),
                    'data'      => array( 'id' => $model->id_membro_cargo ),
                    'success'   => new CJavaScriptExpression(
                        "function( data ) {
                            $( '.row-{$index}' ).remove();
                         }"
                    )

                );
            }
            else {
                $properties[ 'onClick' ] = new CJavaScriptExpression( "$( '.row-{$index}' ).remove();" );
            }

            echo CHtml::button( 'Excluir', $properties );
        ?>
    </div>   
</div>

<hr style="margin-bottom: 7px; margin-top: 7px;">