Multiple ajax function calls?

Hi all,

I’m running into some trouble here. I have a form and I need to change the content of some view elements (e.g. drop down lists) automatically, when the content of one drop down list changes. So I thought about calling two or more functions via ajax. Is it possible and if yes: how can it be done?

Any help, tips, etc. are highly appreciated. :rolleyes:

Thanks in advance,

Thion007

You can do like that:

Create a div (#toreplace in my example) in wich to put all stuffs that have to be ajax-replaced.

You can create your dropdownlist like that:




<?php echo CHtml::activeDropDownList(

	$model,

	'attribute', 

	CHtml::list([...]), 

	array('onChange'=>CHtml::ajax(array(

		'url'=>array('ajax'),

		'replace'=>'#toreplace'

		))));?>



Hi Zaccaria,

thanks for the quick reply.

Do you have a short example on how to fill the div? I don’t get the point at the moment… :blink:

Thanks a lot,

Thio007

First of all, be informed that you are going in one of the most complex and still not completely debugged part of the framework (I mean, clientScript, ajax and so on is not as stabe as you can hope).

You can do something like:

Create a subview like that:




<div id="toreplace">

<?php CHtml::activeDropDownList($model, 'attribute1', CHtml::list([...], array('onChange'=>ajax([...]))))?>

<?php CHtml::activeDropDownList($model, 'attribute2', CHtml::list([...], array('onChange'=>ajax([...]))))?>

<?php CHtml::activeDropDownList($model, 'attribute3', CHtml::list([...], array('onChange'=>ajax([...]))))?>

</div>




In the controller, create an action ajax like that:




public function actionAjax()

{

    // do what you have to do

   $this->renderPartial('subview');

}




The change on the dropDownList will lauch an ajaxRequest to the actionAjax, that will return the subview. This subview will be replaced in your page.

If you have any problem, first of all use firebug, there is a practical console that shows all javascript and so the ajax request.

If you still have problem, try posting again!

Ps: there are even other method, with more javascript and less render. mdomba is the guru about it.