Dropdown Selection In Yii

How to Display Input Element on Dropdown Selection in Yii?

To display an input element on dropdown selection is very easy. We can follow below steps to achieve the goals-

By Using JavaScript:

Selection dropdown 1:

<div class="row">  
   <?php echo $form->labelEx($model,'status'); ?>

   <?php echo $form->dropDownList($model,'status',
      array('ON'=>'ON','OFF'=>'OFF'),
      array('onchange'=>'return checkStatus(this.value)'));

   <?php echo $form->error($model,'status'); ?>
</div>

Hidden div. Will active if selection is true

<div id="hiddendiv" style="display:none">                 
     <div class="row">
         <?php echo $form->labelEx($model,'category'); ?>
         <?php echo CHtml::activeDropDownList($model,'category', 
array('Book'=>'Book','Pen'=>'Pen'), 
array('prompt'=>'Select') ); ?>
         <?php echo $form->error($model,'category'); ?>
     </div>
</div>
function checkStatus(obj)
{
   if(obj=="ON")
   {
     document.getElementById('hiddendiv').style.display="block";
     return false;
   }
   else
   {
     document.getElementById('hiddendiv').style.display="none";
     return false;
   }
}

By Using Yii Ajax-

In View-

<div>
    <?php echo $form->labelEx($model,'status'); ?>

    <?php echo $form->dropDownList($model,'status',
        array('0' =>'In active', '1'=> 'Active'),

        array( 'onChange' => 'javascript:description()', 
                     'ajax'=>array(
                        'type'=>'POST',
                        'url'=>CController::createUrl('controllerName/functionName'),
                        'update'=>'#status_id',
        )));                         
        ?>
     <div id="status_id"></div>
     <?php echo $form->error($model,'status'); ?>
</div>

In Controller-

public function actionfunctionName()
{
    if($_POST['controllerName']['status']=="ON")
    {      
        //For only text field
        //echo CHtml::textField("Employment",'applying_position');

        // For Dropdownlist
        echo $form->dropDownList($model,'status',
        array('Book'=>'Book','Pen'=>'Pen'),
        array('prompt'=>'Select')
        );                         
    }
 }

Hope this will help!