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!


Posted

in

, ,

by

Tags:

Comments

One response to “How to Display Input Element on Dropdown Selection in Yii?”

  1. Gabriele Zoppelletto Avatar
    Gabriele Zoppelletto

    I’m sorry but I can’t use it have try to use it but don’t works, or I can’t undertand correctly where to put the code you’we write.
    Can you expain more clearly where write wot?
    I’ma a new user of Yii and I have some problem to use your code, but I have to use it in my application so I would like to understand. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *