Hi, I want to create multilevel dependent DropDownList.
This are my tables form database.
class{id,class_name}
subjects{id,class_id,name}
sub_section{id,class_id,sub_id,section_name}
sub_topic{id,class_id,sub_id,sub_section_id,topic_name}
While creating records for sub_topic table,
I want to display available classes from class table in a DropDownlist (I am able to do this) and then on selecting a class, I want to display the corresponding subjects(name) from subjects table in second DropDownList ,where subjects.class_id = selected id in the class dropdown (I am also able to do this),
Now on selecting a subject, I want to display the corresponding section(section_name) from sub_section table in third DropDownList ,
where sub_section.sub_id = selected id in the subjects dropdown.
here i am getting third dropdown empty.
This is my code
[size="2"]View file[/size]
<div class="row">
<?php echo $form->labelEx($model,'class_id'); ?>
<?php
$classArray = CHtml::listData(Classes::model()->findAll(),'id','class_name');
echo $form->DropDownList($model,'class_id',$classArray,
array(
'prompt'=>'Select Class',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('SubTopic/dynamicSubjects'),
'update'=>'#'.CHtml::activeId($model,'sub_id')
)));
?>
<?php echo $form->error($model,'class_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'sub_id'); ?>
<?php echo $form->dropDownList($model,'sub_id',array(
'prompt'=>'Select Subject',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('SubTopic/dynamicSubSection'),
'update'=>'#'.CHtml::activeId($model,'sub_section_id')
))); ?>
<?php echo $form->error($model,'sub_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'sub_section_id'); ?>
<?php echo $form->dropDownList($model,'sub_section_id',array()); ?>
<?php echo $form->error($model,'sub_section_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'topic_name'); ?>
<?php echo $form->textField($model,'topic_name',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'topic_name'); ?>
</div>
[size="2"]My Controller file SubTopicController.php[/size]
public function actionDynamicSubjects() {
$class_id = $_POST['SubTopic']['class_id'];
$data=Subjects::model()->findAll('class_id=:class_id',
array(':class_id'=> $class_id));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name) {
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
public function actionDynamicSubSection() {
$sub_id = $_POST['SubTopic']['sub_id'];
$data=SubSection::model()->findAll('sub_id=:sub_id',
array(':sub_id'=> $sub_id));
$data=CHtml::listData($data,'id','section_name');
foreach($data as $value=>$section_name) {
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($section_name),true);
}
}