Hot To Update Dropdownlist With Ajax Request ?

I want to create 2 dropDownList

First dropDownList is Hour

Second dropDownList is Minute dependent on Hour

I used ajax request but not work.

index.php




<?php

$this->breadcrumbs=array(

    'Book',

);

?>


<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(

    'id'=>'test',

    'type'=>'horizontal',

    'enableClientValidation'=>true,

    'clientOptions'=>array(

        'validateOnSubmit'=>true,

    ),

)); ?>


<div class="row">


    <?php

    echo $form->dropDownList( $model, 'C_time',$this->HH(), array( 'empty' => 'Select Hour ',

            'ajax' => array(

                'type'=>'POST',

                'url'=>CController::createUrl('CustomersController/MM'),

                'update'=>'#Minute',

                'data'=>array('hour' => 'js:this.value'),

            ),

        )

    );

    echo CHtml::dropDownList('Minute','', array('empty'=>'Select Minute'));

    ?>

</div>


<?php $this->endWidget(); ?>



CustomersController




public function actionIndex()

{

    $model = new Customers();        

    $this->render('index',array('model'=>$model));

}


public function HH()

{

    $r_model = new Restaurant();

    $par1 = 'HOUR';

    $par2 = 'R_open';

    $open = $r_model->getTime($par1,$par2);

    $par2 = 'R_close';

    $close = $r_model->getTime($par1,$par2);

    $rs = array();

    for ($i = $open; $i < $close; $i++){

        $rs[$i] = $i;

    }

    return $rs;

}


public function actionMM()

{

    if(isset($_POST['hour']) && $_POST['hour']!=''){

        $this->render('test');

        $hour=$_POST['hour'];

        $r_model = new Restaurant();

        $start = '00';

        $end = '60';

        if($hour == $r_model->getTime('HOUR','R_open')){

            $par1 = 'MINUTE';

            $par2 = 'R_open';

            $open = $r_model->getTime($par1,$par2);

            $start = $open;

        }

        elseif($hour == $r_model->getTime('HOUR','R_close')){

            $par1 = 'MINUTE';

            $par2 = 'R_close';

            $close = $r_model->getTime($par1,$par2);

            $end = $close;

        }

        $rs = array();

        for ($i = $start; $i < $end; $i++){

            $rs[$i] = $i;

        }

        echo CHtml::tag('option',array('value' => ''),

            CHtml::encode('Select User Type'),true);

        foreach($rs as $id)

        {

            echo CHtml::tag('option',

                array('value' => $id),CHtml::encode($id),true);

        }

    }

}



Add this code to your success call in 1st ajax, idk but update works strange for me.




'success'=>'js:function(data){

   $("#2ndDropdownid").empty();

   $("#2ndDropdownid").append(data);

}'



Thank. Now I’m success

index.php




<?php

$this->breadcrumbs=array(

    'Book',

);

?>


<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(

    'id'=>'test',

    'type'=>'horizontal',

    'enableClientValidation'=>true,

    'clientOptions'=>array(

        'validateOnSubmit'=>true,

    ),

)); ?>


<div class="row">


    <?php

    echo $form->dropDownList( $model, 'C_time',$this->HH(), array( 'empty' => 'Select Hour ',

            'ajax' => array(

            'type'=>'POST',

            'url'=>CController::createUrl('Customers/MM'),

            'update'=>'#drpMinute',

            'data'=>array('hour' => 'js:this.value'),

        )

    );

    echo CHtml::dropDownList('Minute','', array('empty'=>'Select Minute'));

    ?>

</div>


<?php $this->endWidget(); ?>



CustomersController




public function actionIndex()

{

    $model = new Customers();        

    $this->render('index',array('model'=>$model));

}


public function HH()

{

    $r_model = new Restaurant();

    $par1 = 'HOUR';

    $par2 = 'R_open';

    $open = $r_model->getTime($par1,$par2);

    $par2 = 'R_close';

    $close = $r_model->getTime($par1,$par2);

    $rs = array();

    for ($i = $open; $i < $close; $i++){

        $rs[$i] = $i;

    }

    return $rs;

}


public function actionMM()

{

    if(isset($_POST['hour']) && $_POST['hour']!=''){

        $hour=$_POST['hour'];

        $r_model = new Restaurant();

        $start = '00';

        $end = '60';

        if($hour == $r_model->getTime('HOUR','R_open')){

            $par1 = 'MINUTE';

            $par2 = 'R_open';

            $open = $r_model->getTime($par1,$par2);

            $start = $open + 1;

        }

        elseif($hour == $r_model->getTime('HOUR','R_close')){

            $par1 = 'MINUTE';

            $par2 = 'R_close';

            $close = $r_model->getTime($par1,$par2);

            $end = $close;

        }

        $rs = array();

        for ($i = $start; $i < $end; $i++){

            if($i < 10 && $i > 0)

                $rs[$i] = '0'.$i;

            else

                $rs[$i] = $i;

        }

        echo CHtml::tag('option',array('value' => ''),

            CHtml::encode('Select Minute'),true);

        foreach($rs as $id)

        {

            echo CHtml::tag('option',

                array('value' => $id),CHtml::encode($id),true);

        }

    }

}