how to do calculation in yii

how i do calculation in yii (add value of two text field and display in 3rd textbox)

What U need here is js. Attach onchange on both test fields, when values are changed collect values from both fields, calcucate result and populate 3rd field with result.

1 Like

I wrote this code in _form it call javascript function but not working after alert


_form.php

<script type = "text/javascript">

function clickedVal()

{

     alert('hi');


     var y = document.getElementById(&quot;C&quot;).value;


     var z = document.getElementById(&quot;Cpp&quot;).value;


     var x = y + z;


     document.getElementById(&quot;Total&quot;).innerHTML = x;

}

</script>

<div class="form">

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

'id'=&gt;'result-form',


'enableAjaxValidation'=&gt;false,

)); ?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;





&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





&lt;div class=&quot;row&quot;&gt;


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


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


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'Name_',array('size'=&gt;15,'maxlength'=&gt;15)); ?&gt;


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


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


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


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


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


&lt;/div&gt;








&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'Total',array('onfocus'=&gt;'javascript:clickedVal(this);')); ?&gt;		


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


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


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


&lt;/div&gt;





&lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton(&#036;model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;


&lt;/div&gt;

<?php $this->endWidget(); ?>

</div><!-- form -->

Are you sure that C, CPP and Total are the ids of these fields? Yii gives automatically ids like "Model_attribute". Right click on the navigator and "view source" to see which ids you have.

Thanks a lot now it works proper. Thanks once again.

Ids are not C and Cpp, but YourModelName_C and YourModelName_Cpp, so change selectors and this should work

1 Like

yes thats work now thanks