sandeep
sandeep

Reputation: 95

ajax in depended dropdownList in yiii not working

just implemeting depended dropDownList in yii. its view part is-

 echo $form->dropDownList($model, 'standard', $listStandard, array(
            'empty' => 'Select standard',
            'ajax'=>
            array(
                'type'=>'POST',
                'url'=>CController::createUrl('scholarship/updateDivision'),
                'update'=>'#updatedDivision',
                //'data'=>array('std'=>'js:this.value'),
            )
            ));

<?php echo CHtml::dropDownList('updatedDivision','',array('1'=>'1'),array()); ?>

its controller part

public function actionUpdateDivision(){
        echo CHtml::dropDownList('updatedDivision','',array('1'=>'hello','2'=>'2'),array());
}

here request is not passing to controller.can you find any problem in this code? Thanks in advance..

Upvotes: 1

Views: 85

Answers (1)

Igor Savinkin
Igor Savinkin

Reputation: 6267

According to this post the controller action must be smth. as following:

$data=Location::model()->findAll('parent_id=:parent_id', 
              array(':parent_id'=>(int) $_POST['country_id']));

$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
    echo CHtml::tag('option',
               array('value'=>$value),CHtml::encode($name),true);
}

Obviously, when you update the DOM element in view id='updatedDivision' with something also having id='updatedDivision' from controller:

echo CHtml::dropDownList('updatedDivision','',array('1'=>'hello','2'=>'2'),array());

it won't work as desired.

Try this in controller/action:

foreach(array('1'=>'hello','2'=>'2') as $value=>$name)
{
    echo CHtml::tag('option',
               array('value'=>$value),CHtml::encode($name),true);
}

Update

Also check with web tools (F12, Ctrl+Shift+I) if an ajax XHR is properly formed and response is sent back.

Upvotes: 1

Related Questions