Hi,
I am trying to create dependent dropdowns, where the second dropdown gets filled based on the first dropdown value chosen.
I have read through http://www.yiiframework.com/wiki/24/creating-a-dependent-dropdown#hh0 and tried using that technique but it the second dropdown still does not get populated. I have also looked at other websites as well, but still doesnt work.
What am I doing wrong?! Please help!
I have 3 relevant tables:
Escline - esc_line_id, name
ThawPassage - thaw_passage_id, esc_line_id, passage_num
ChimeraExperiment - chimera_experiment_id, esc_line_id, passage_num
Here is the code I used for the form:
<div class="row">
<?php echo $form->labelEx($model,'esc_line_id');
$esc_order = new CDbCriteria;
$esc_order->order = 'name ASC';
echo $form->dropDownList($model,'esc_line_id',CHtml::listData(EscLine::model()->findAll($esc_order),'esc_line_id','name'),
array(
'prompt' => ' ',
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('ChimeraExperiment/Dynamicpassage'),
'update' => '#passage_num'
)
)
);
?>
<?php echo $form->error($model,'esc_line_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'passage_num');
echo $form->dropDownList($model,'passage_num',array());
echo $form->error($model,'passage_num');
?>
</div>
Here is the code I used for the Controller:
public function actionDynamicpassage()
{
$data = ThawPassage::model()->findAll('esc_line_id=:parent_id',
array(':parent_id'=>(int) $_POST['ChimeraExperiment']['esc_line_id']));
$data = CHtml::listData($data,'passage_num','passage_num');
foreach($data as $id => $value)
{
echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);
}
I have also added the following to the accessRules function of the Controller:
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','dynamicPassage'),
'users'=>array('@'),
),
Thank you!