Multiple Dependant Dropdownlist

i have 3 dependent dropdownlist: cycle, behavior and behavior type

to get the value of the behavior, i have to select both cycle and behavior type.

does any one has an example on this. i don’t know even how to write the code

a behavior list based on the value of the cycle and the type

thank you in advance

Hi freshyiiuser

One way is with ajax.

on change behavior or behavior type make a request on server (through Controller/action) and fetch the new values

another way is by javascript (if there are not many values in total). Loads all values in an array of javascript and their relation with behavior and behavior type. Write onchange events javascript that modify the others dropdownlist values

thank you for your reply, but i didn’t understand what you say

can you please provide me more information or example

NB: i have in my form 2 other dropdownlist dependent on the cycle value

so actually, cycle value affect the class, the matiere and the behavior

then the behavior value depends on the cycle and the type value

here is my code:




<span>Cycle:</span>	

		<div style="width:350px;" class="combostyle">

		

		<?php

			echo CHtml::dropDownList('CycleID','', CHtml::listData(Usersectionsecurity::model()->findAll(), 'Group_Code', 'E_Group_Desc'),

				array(

					'prompt'=>'Select Cycle',

					  'ajax' => array(

                        'type'=>'POST',

                        'url'=>CController::createUrl('Usersectionsecurity/getClassByCycle'), 

                        'dataType'=>'json',

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

                        'success'=>'function(data) {

                            $("#stclass").html(data.dropdownclasses);  // related to the class

                            $("#periodID").html(data.dropdownperiod);  // related to the period/matiere

                        }',

            ))); 

								

		?>

		</div>


<span>Select Type</span>

			<div style="width:300px" class="combostyle">

			<?php

				echo $form->dropDownList($model,'BehavType',Yii::app()->params['bType'],

							array(

							'prompt'=>'Select Behavior Type',

							'ajax' => array(

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

								'url'=>CController::createUrl('Assignment/getBehaviorGroup'), //url to call.

								'update'=>'#'.CHtml::activeId($model,'behavGroupID'), 

							))); 

			?>

			</div>


<span>Behavior:</span>	

		<div style="width:930px;" class="combostyle">

		<?php

			echo $form->dropDownList($model,'behavID', array(),array('prompt'=>'Select Behavior'));

			echo $form->error($model,'behavID');

		?>

		</div>



in the controller:




public function actionGetClassByCycle()

	{

		$type = Yii::app()->user->getType();

		$userID = Yii::app()->user->getID();

		

		if($type == 'Teacher'){

			$model = Stclass::model()->GetClassOfUser($userID, $_POST['CycleID']);

			

			$data=CHtml::listData($model,'Class_Code', 'A_Class_Desc');

		}else{

			$model=Usersectionsecurity::model()->findAll('Group_Code=\'' . $_POST['CycleID'] . '\'');

			

			$data=CHtml::listData($model,'Class_Code', 'A_Class_Desc', 'A_Major_Desc');

		}

		

		$dropdownclasses = "<option value=''>Select Class </option>";


		foreach($data as $parent=>$child)

		{

			$dropdownclasses .= '<optgroup label="' . $parent . '">';

			//echo '<b>' . CHtml::tag('option',array(), CHtml::encode($parent)) . '</b>';

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

			{				

				$dropdownclasses .=  CHtml::tag('option',array('value'=>$value), CHtml::encode($name),true);

			}

		}

		           

			$model2= Matiere::model()->GetMainSubjectByCycle($_POST['CycleID']);

			$data2 = CHtml::listData($model2,'Subject_Code', 'E_Subject_Name'); 

			

			$dropdownperiod =  "<option value=''>Select Period </option>";

			

			foreach($data2 as $value2=>$name2)

			{

				$dropdownperiod .=  CHtml::tag('option',array('value'=>$value2), CHtml::encode($name2),true);

			}

			

			$dropdowngroup = "<option value='null'>Select Behavior Group</option>";

            // return data (JSON formatted)

            echo CJSON::encode(array(

              'dropdownclasses'=>$dropdownclasses,

              'dropdownperiod'=>$dropdownperiod,

              'dropdowngroup'=>$dropdowngroup

            ));

	}


	public function actionGetBehaviorGroup()

	{

		$assignment = Assignment::model()->GetAssignmentInfo($_POST['Assignment']['BehavType']);

		

		$data=CHtml::listData($assignment,'ID','E_BGName');

		

		echo "<option value=''>Select Behavior Group </option>";

		

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

		{

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

			echo "<option value='".$value."'>". $name . "</option>";

		}

	}



how to write what you said???

do you have an example that clarify this example?

is there any help here. i really need it urgentlyy

Hi my friend

you are close on the solution (first way)

because your code is a little large, tell us what is the problem,

check also with firebug (network tab) the ajax response if it is the right for your updates

there is no error in my code, the problem that my code is not complete

this code enable me to select the behavior group based on the value of the behabior type

i need to edit this code in order to get the behavior group value based on the selection of the behavior type and the cycle

is there any example that help me to do so?

thank yuu