How to call a Controller action from an onChange event?

Hello Yii Forum friends. I’ve been enjoying Yii I am fairly new but I have done some development. This is my first post! I need some help to know how can I call a Controller action from a textbox onChange event?

Thanks for any help.

Quick update because maybe I was not clear:

I have a form built with CActiveForm. This form has many text-boxes. When I change any value other must be updated computing a formula using the new entered value.




My View:

<tr>

  <td>Average KW Demand:</td>

  <td><?php echo $form->textField($lfac_model, 'kw_demand', array('size'=>5));?></td>

</tr>

<tr>

  <td>Estimated Load Factor:</td>

  <td><?php echo $form->textField($lfac_model, 'loadf', array('size'=> 5));?>/td>

</tr>



When the user changes “Estimated Load Factor”, “Average KW Demand” value should be updated with the result of a formula. What I’ve figured out is the following solution:

  • To store the formulas in a table

  • Then go to a Controller action, retrieve the specific formula

  • Calculate the new value and send it back to the view

I’m not sure if this is the best idea to implement this. But according to my experience this is what I’ve thought. Any help would be appreciated.

If it is helpful for someone this is what I did:




View:

<td>Average KW Demand:</td>

<td><?php echo $form->textField($lfac_model, 'kw_demand', array('id'=>'kwdemand_id', 'name'=>'kwdemand', 'size'=>5)); ?></td>


<td>Estimated Load Factor:</td>

<td>

<?php

  echo $form->textField($lfac_model, 'loadf', array(

    'ajax' => array(

      'type'=>'POST', //request type

      'url'=>$this->createUrl('Calculator/test'), //url to call

      'success'=>'js:function(data) { $("#kwdemand_id").val(data); }',

    )

  ));

?>

</td>






Controller:

public function actionTest() {

  [get formula from database, calculate new value]

  echo $kw_demand;

  Yii::app()->end();

}



Thanks I was looking for this!

Hi,

I am also very new to this framework

so we can use

[color=#000000] [/color][color=#008800]‘ajax’[/color][color=#000000] [/color][color=#666600]=>[/color][color=#000000] array[/color][color=#666600]([/color][color=#000000]

  [/color][color=#008800]'type'[/color][color=#666600]=&gt;[/color][color=#008800]'POST'[/color][color=#666600],[/color][color=#000000] [/color][color=#880000]//request type[/color][color=#000000]


  [/color][color=#008800]'url'[/color][color=#666600]=&gt;[/color][color=#000000]&#036;this[/color][color=#666600]-&gt;[/color][color=#000000]createUrl[/color][color=#666600]([/color][color=#008800]'Calculator/test'[/color][color=#666600]),[/color][color=#000000] [/color][color=#880000]//url to call[/color][color=#000000]


  [/color][color=#008800]'success'[/color][color=#666600]=&gt;[/color][color=#008800]'js:function(data) { &#036;(&quot;#kwdemand_id&quot;).val(data); }'[/color][color=#666600],[/color][color=#000000]


[/color][color=#666600])[/color]

for any ajax call with any element.