Calculate on fly

Hi guys,

I’m newbie and learning Yii so I have a simple question for which I need solution/quid like how to calculate two field on fly and store result somewhere on same form like…(price x qty = total).




<div class="row">

	<?php echo $form->labelEx($model,'price'); ?>

	<?php echo $form->textField($model,'price'); ?>

	<?php echo $form->error($model,'price'); ?>


<div class="row">

	<?php echo $form->labelEx($model,'qty'); ?>

	<?php echo $form->textField($model,'qty'); ?>

	<?php echo $form->error($model,'qty'); ?>


<div class="row">

	<?php echo $form->labelEx($model,'total'); ?>

	<?php echo $form->textField($model,'total'); ?>

	<?php echo $form->error($model,'total'); ?>


Please help in this…

You can calculate it this way, in the controller

$model->total = $model->price * $model->qty;

Enable ajax=true in you form widget. However, your total cannot be a textField as user can override the calculation

2nd method, use jQuery to computer

Hi Pradhan,

First thanks for your your quick reply and as me newbie, I just did enable the Ajax in widget on order_form and about this code

 $model->total = $model->price * $model->qty;

where exactly I have to write and also how and where to write this

 2nd method, use jQuery to computer

I look forward to hear again.

In this way you have to have total field in your table in database.

You can calculate total price like:

$this->render('view',array('model'=>$model,'total' => $model->price * $model->qty));

In view you can use total like:

<?php echo $total; ?>

Second way directly in view

<?php echo $model->price * $model->qty; ?>

Thank qwerty for your reply but there is a problem, a user can change Quantity on Order_form view so by doing this I need to update the Total some how on Order_form view.

I look forward to get some solution here.

You can use ajax or javascript.

Please see this. Though it is not exactly what you are looking for, it may give you an idea how to implement "calculation ob fly". In this link, it is discussed, how to refresh the second dropdown list when a new option is selected in the first dropdown list.

Thanks tax14 for the desire link and I give try to implement that idea but can’t get the desire result please somebody have a look into this code and could guide me if there is something missing/need to update the code to get the result.

My code in NewOrder view is…

<div class="row">					


  echo $form->DropDownList($neworder,'quantity',



		       'ajax' => array(



		       'update'=>'#' .CHtml::activeId($neworder,'total_amt')




<div class="row">											

   <?php echo $form->textField($neworder,'total_amt'); ?>


My code in Controller is…

        public function actionDynamicTotal() {

            $user_qty = (int) $_POST['quantity'];


            $result=$user_qty * $price;

                echo $result;


Please somebody have a look and guide to get the Result back to Neworder view.

Thanks in advance.

Could you solve this for me ? Please. I can pay for it. I need it so much.
Thank you.