how i do calculation in yii (add value of two text field and display in 3rd textbox)
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.
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("C").value;
var z = document.getElementById("Cpp").value;
var x = y + z;
document.getElementById("Total").innerHTML = x;
}
</script>
<div class="form">
<?php $form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'result-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'Roll_no'); ?>
<?php echo $form->textField($model,'Roll_no'); ?>
<?php echo $form->error($model,'Roll_no'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Name_'); ?>
<?php echo $form->textField($model,'Name_',array('size'=>15,'maxlength'=>15)); ?>
<?php echo $form->error($model,'Name_'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'C'); ?>
<?php echo $form->textField($model,'C'); ?>
<?php echo $form->error($model,'C'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Cpp'); ?>
<?php echo $form->textField($model,'Cpp'); ?>
<?php echo $form->error($model,'Cpp'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Total'); ?>
<?php echo $form->textField($model,'Total',array('onfocus'=>'javascript:clickedVal(this);')); ?>
<?php echo $form->error($model,'Total'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Percentage'); ?>
<?php echo $form->textField($model,'Percentage'); ?>
<?php echo $form->error($model,'Percentage'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?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
yes thats work now thanks