dependent dropdown

Like the dependent.

How can create dependent dropdown lists, at same time for two other fields, when a parent is chosen.

The two other fields depends only to parent choice

Hi,

in the post http://www.yiiframework.com/wiki/24/creating-a-dependent-dropdown/#c4447, you have 2 fields updated from one.

You can adapt it.

I tried this but nothing…Is it possible a step by step answer

Give us your code (a part of it at least): controller function and form.

Controller


	public function actionUpdateST()

	{

		$data=Student::model()->findAll('depid=:depid',

				array(':depid'=>(int) $_POST['depid']));

		$data=CHtml::listData($data,'id','name');

		foreach($data as $value=>$name)

		{

			$dropDownA .= CHtml::tag('option',

					array('value'=>$value),CHtml::encode($name),true);

		}

		

		$dataB=Lesson::model()->findAll('depid=:depid',

				array(':depid'=>(int) $_POST['depid']));

		$dataB=CHtml::listData($dataB,'id','title');

		foreach($dataB as $value=>$name)

		{

			$dropDownB .= CHtml::tag('option',

					array('value'=>$value),CHtml::encode($name),true);

		}

		echo CJSON::encode(array(

				'dropDownA'=>$dropDownA,

				'dropDownB'=>$dropDownB

		));

	}

View


array(

				'ajax' => array(

				'type'=>'POST',

				'dataType'=>'json',

				'url'=>CController::createUrl('updatest'),

 				'data'=>array('depid'=>'js: $(this).val()'),

				'success'=>'function(data) {

						$("#CP_studentid").html(data.dropdownA);

 						$("#LS_lessonid").html(dataB.dropdownB);

						}',

				))); 

It this a typo?


$("#LS_lessonid").html(dataB.dropdownB);

I guess there’s no data[color="#FF0000"]B[/color]

Also it’s preferable you respect the case, if you write


echo CJSON::encode(array(

                                'dropDownA'=>$dropDownA,

                                'dropDownB'=>$dropDownB

                ));

It should also be


                                'url'=>CController::createUrl('updateST'),

                                'success'=>'function(data) {

                                                $("#CP_studentid").html(data.dropDownA);

                                                $("#LS_lessonid").html(data.dropDownB);

                                                }',

And are you sure of your div ids? #CP_studentid and #LS_lessonid?

Bottom line: Firebug would help you a lot debugging (in case of Ajax request, check XHR tab)

So the controller should be like this?:


public function actionUpdateST()

        {

                $data=Student::model()->findAll('depid=:depid',

                                array(':depid'=>(int) $_POST['depid']));

                $data=CHtml::listData($data,'id','name');

                foreach($data as $value=>$name)

                {

                        $dropDownA .= CHtml::tag('option',

                                        array('value'=>$value),CHtml::encode($name),true);

                }

                

                $data=Lesson::model()->findAll('depid=:depid',

                                array(':depid'=>(int) $_POST['depid']));

                $data=CHtml::listData($data,'id','title');

                foreach($data as $value=>$name)

                {

                        $dropDownB .= CHtml::tag('option',

                                        array('value'=>$value),CHtml::encode($name),true);

                }

                echo CJSON::encode(array(

                                'dropDownA'=>$dropDownA,

                                'dropDownB'=>$dropDownB

                ));

        }

and View:


array(

                                'ajax' => array(

                                'type'=>'POST',

                                'dataType'=>'json',

                                'url'=>CController::createUrl('updatest'),

                                'data'=>array('depid'=>'js: $(this).val()'),

                                'success'=>'function(data) {

                                                $("#CP_studentid").html(data.dropdownA);

                                                $("#LS_lessonid").html(data.dropdownB);

                                                }',

                                ))); 

The divs are ok.