ajax dropdown n the controller

Hi Guys,

I want to generate the following dropdownlist from the controller:


echo Chtml::dropDownList('book_id','', $booksList,

			array(

			'ajax' => array(

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

			'url'=>'/index.php?r=book/dynamicauthor' ,

			'update'=>'#author',

			'data'=>array('book_id'=>'js:this.value'),

			))); //selector to update)); 	

But the url never call the action "dynamicauthor", but when i put this code in my view it works.

I know its not a good approach to put this in the controller, but for what i want to do, i have no choice, because this code must be generated and added dynamically when i click on an action from the view.

Im able to use this code and its works :


echo '<select id="Book_book_id" name="Book[book_id]">';

foreach($books2 as $id=>$value){

		{	

    echo CHtml::tag('option',array('value' => $value['id']),CHtml::encode($value['name']),true);


}

echo '</select>';

but how can i use Ajax within the CHtml::tag ?

Thanks

whats the generated JS when called from the controller? having this code in the controller shouldnt be an issue.

Hi,

The JS doesn’t generate the right code, it should be something like:




jQuery('body').delegate('#book_book_id','change',function(){jQuery.ajax({'type':'POST','url':'/index.php?r=book/dynamicauthor','data':{'book_id':this.value},'cache':false,'success':function(html){jQuery("#author").html(html)}});return false;});

but its empty, and only the dropdownlist html code is generated and not the JS code :(

Hello,

why dont you separate the js code and your view. then register your js file with Yii::app()->getClientScript->registerScriptFile.

or




echo Chtml::dropDownList('book_id','',           $booksList,array('onchange'=>'doSomething(this.value)'));




then in JS file write the function




function doSomething(value){

//write your ajax request here

}