Dependent Dropdowns

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!




        array(

            'prompt' => ' ', 

            'ajax' => array(

               'type' => 'POST',

 this ===>     'url' => CController::createUrl('ChimeraExperiment/Dynamicpassage'),

               'update' => '#passage_num',

            ),       

         ),



Just looking over this quickly: Does this line give you the "correct" address to the proper controller/action?

The action function is in the Controller called ChimeraExperimentController.

Just tried it with “(‘ChimeraExperimentController/Dynamicpassage’)” instead… but still doesnt work (second dropdown still doesnt get filled).

What errors are you getting? If you’re not getting any errors, have you checked the response through your browser’s developer tools? Just check the response to your ajax request.

I wasnt getting any errors. I did find out the problem though.

This code




$data = CHtml::listData($data,'passage_num','passage_num');



should have been




$data = CHtml::listData($data,'thaw_passage_id','passage_num');



because thaw_passage_id is the id of the table. Whoops.

Thank you to everyone that responded :)

I would like to get a better understanding of this with a simple example that receives data from a database and stores the result in another table. Let’s say I have a database table and model of states, (id, state_name), and a table & model of cites, (id, city_name, state_id). I want to use dependent dropdowns in a form to add the names of the state & city to a Location model(id, state, city).

Would I use a form model that extends CForm instead of the one created by gii crud generator for the Location model? Also, does the state_id attribute in the city table have to be a foreign key to state.id? I would guess that the answer to both would be yes, but I have not seen it laid out explicitly.

You can try the extension cascadedropdown.