I need some help with Yii.
How can I call a Controller action from a textbox onChange event?
Update:
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. So what I am trying to do is the following:
<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 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.
You should be able to use the CJuiAutoComplete Yii widget to do what you want. There is a basic demo here and there are other examples in the Yii docs. The main thing you would need to make sure in your case is that you also specify the “appendTo” option, see the jQuery autocomplete docs, which that widget uses.
So basically, you would set up the widget for your “Estimated Load Factor” field, and point to the “Average KW Demand” field via the appendTo attribute.
And if you don’t want the autocomplete stuff and simply want to replace the value, you can use the htmlOptions attribute “ajax” on your “loadf” value like so:
see: CHtml#ajax-detail and jQuery.ajax. In this case you would most likely return plain text so your controller function would look like: