title: Yii populate a dropdown on select of another
body: <p>I have a dropdown that I want to populate when an item in another dropdown is selected. Both the dropdown are tied to data/model passed on from controller. and the first dropdown is populated from DB by calling a function in the model. Heres' the form,</p>
  
  <pre><code>echo $form-&gt;dropDownListRow($modelunit, 
          'superunit',
          $model-&gt;getSunits(), 
          array(
          'ajax' =&gt; array(
          'type'=&gt;'POST',
          'url'=&gt;CController::createUrl('user/getunits'),
          'update'=&gt;'#unit_id',
          ))); 
  
  echo CHtml::dropDownList('unit_id','', array());
  </code></pre>
  
  <p>Here's the action user/getunits called by Ajax.</p>
  
  <pre><code>$data=Unit::model()-&gt;findAll('sid=:sid', 
                    array(':sid'=&gt;(int) $_POST['superunit']));
  
      $data=CHtml::listData($data,'id','name');
      foreach($data as $value=&gt;$name)
      {
          echo CHtml::tag('option',
                     array('value'=&gt;$value),CHtml::encode($name),true);
      }
  </code></pre>
  
  <p>I keep getting an error "Undefined index: superunit" when first dropdown is selected. Also, you may notice I am using form->dropDownListRow for the first dropdown while using CHtml::dropDownList for the second. That's cause I am clueless on the syntax of how exactly to make sure the dropdown is populated correctly with ajax and at also properly bind to the model. </p>