Dependent dropdown for country and state in yii 1.13

I had created two database tables as,



In Country table i had countryid & cname field. In State table i had state_id, state_name & country_cid.

Now i need to make a dependent dropdown i.e. when i select country dropdown box, then the particular state for that country should be displayed. I had done below coding. But it displays only country and not displays state.

This is in views/sample/register.php file.

<div class="row">

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

    $opts = CHtml::listData(Country::model()->findAll(),'countryid','cname');

    echo $form->dropDownList($model,'country_id',$opts,


                'prompt'=>'Select Country',

                'ajax' => array(




                 'dataType' => 'JSON',


                 . '{'

                 .  'var html="<option value=>-----Select city-----</option>";'

                 .  '$.each(data,function(i,obj)'

                 .  '{'

                 .      'html+="<option value=\'""\'>""</option>";'

                 .  '});'

                 .  '$("#state_id").html(html);'

                 .  '}'


    echo $form->error($model,'country_id'); ?>


<div class="row">

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

    echo CHtml::dropDownList('state_id','', array());

    echo $form->error($model,'state_id'); ?>


This is in controllers/SampleController.php file

public function actiondynamicSubcategory()



        $criteria=new CDbCriteria();




        $cityAry= State::model()->findAll($criteria);


        foreach($cityAry as $i=>$obj)





        echo json_encode($ary);


I had created Country model & State model. I had analyzed many sites. But i can’t get right. Please anybody help.