Yii: use ajax function via htmlOptions attribute

If you want to trigger an ajax call on a Yii form element onchange, you can pass an array of ajax attributes as one of the “htmlOptions” like so:

<?php echo $form->textField($model,'my_input_field',array(
	'ajax' => array(
		'type'=>'POST', //request type
		'url'=>$this->createUrl('site/ajaxAction'), // url to call controller action
		'success'=>' function(data) { $(\'#my_output_field\').val(data) }',// function to call onsuccess 
             // "data" is returned data and function can be regular js or jQuery
             // here we are are updating the value of another field that has id "my_output_field"
	),
	'size'=>60,
	'maxlength'=>128,
)); ?>

and the (site) controller action would look something like:

public function actionAjaxAction() {
  [check/filter inputs...]
  [data functions...]
  echo $my_val_string;
  Yii::app()->end()
}

No Responses to “ “Yii: use ajax function via htmlOptions attribute”

Trackbacks/Pingbacks

  1. Yii: use ajax function via htmlOptions attribute « Sochinda's Blog - [...] inputs...] [data functions...] echo $my_val_string; Yii::app()->end() } ¬†Referenced by:¬†http://tech.ldg.me/2011/09/yii-use-ajax-call-via-htmloptions-attribute Like this:LikeBe the first to like …

Leave a Reply