Hi, I’m working with a form that has a dependent dropdown, the user has to select the organization_id and when the organization_id has been selected, the branch_id dropDownList has to be updated (with the branches linked to the selected organization).
Here’s the view’s code:
<?php
echo CHtml::dropDownList('organization_id','',
CHtml::listData(Organization::model()->findAll(),'id','name'),
array(
'prompt'=>'Select Organization','span'=>8,
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('user/dynamicBranch'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#branchId', //selector to update
'data'=>array('organization_id'=>'js:this.value'), //'js:javascript statement'
//leave out the data key to pass all form values through
)));
?>
<br/>
<?php echo $form->dropDownList($model,'branch_id', array(),
array('prompt'=>'Select the Branch...','span'=>5,
'id'=>'branchId',)); ?>
The controller’s method code is:
public function actionDynamicBranch()
{
$data = Branch::model()->findAll('organization_id=:organizationId',
array(':organizationId'=>(int)$_POST['organization_id']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
The accessRules for the controller:
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','dinamycBranch'),
'users'=>array('@'),
),
I don’t know what I’m doing wrong, I have done this before but the only diff is that in this solution, I’m using YiiWheels and the app that is working fine is using just YiiBootstrap.
Thanks in advance.