Insert data into Dropdownlist by ajax request in yii

This turorial have the details of dynamic data insertion to dropdownlist using ajax. I added the source code below for this concept. We have two dropdownlist in form “usercategory”,”usertype”. When we change the usercategory, usercategory related usertype will show in “usertype” dropdownlist.

In view.php

<div class="row">
   <?php echo $form->labelEx($model,'usercategory'); ?>
   <?php $categories=Yii::app()->Datacomponent->usercategory();
   echo $form->dropdownlist($model,
                          array('empty'=>'Select Category ',
  <?php echo $form->error($model,'usercategory'); ?>
<div class="row">
 <?php echo $form->labelEx($model,'usertype'); ?>
 <?php echo form->dropDownList($model,
                                 array('empty'=>'Select Type')); 
<?php echo $form->error($model,'usertype'); ?>

In controller.php

class SiteController extends Controller {
    public function actionUsertype(){
     if(isset($_POST['categoryid']) && $_POST['categoryid']!=''){
            echo CHtml::tag('option',array('value' => ''),
                               CHtml::encode('Select User Type'),true);
            foreach($data as $id => $value)
              echo CHtml::tag('option',array('value'=>$id),CHtml::encode($value),true);

Leave a Reply

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