How to get value of DropdownList in controller?

Hi ,

I have a CHtml::dropDownList in my View and I want to catch the current selected value in my

controller , how can I do that ?




echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),

array(

'ajax' => array(

   'type'=>'POST', //request type

   'url'=>Yii::app()->baseUrl.'/index.php?r=allownce/dynamiccities', //url to call

   'update'=>'#city_id', //selector to update

 //'data'=>'js:javascript statement' 

 //leave out the data key to pass all form values through

))); 

 

//empty since it will be filled by the other dropdown

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



What code do it need to put in my controller class action function ?

I am very sure that I made a success POST to the allownce/dynamiccities function but

I have tried $_POST[‘country_id’] but value was not captured.

Thanks for any sharing.

Hi ,

I managed to do it and this are some of the changes

  1. I disable the filters in my controller for temporary testing

    //‘accessControl’, // perform access control for CRUD operations

  2. I code the controller action function as follow


	public function actionDynamiccities()

   {


	if ($_POST['country_id'] == '1') {

		echo CHtml::tag('option',

	                array('value'=>'1') , CHtml::encode($_POST['country_id']),true);

	    echo CHtml::tag('option',

	                array('value'=>1) , CHtml::encode('test11'),true);

					}

	else 

	if ($_POST['country_id'] == '2') {

	    echo CHtml::tag('option',

	                array('value'=>2) , CHtml::encode('test2'),true);

		echo CHtml::tag('option',

	                array('value'=>2) , CHtml::encode('test22'),true);

					

					}

	else if ($_POST['country_id'] == '3') {

	echo CHtml::tag('option',

	                array('value'=>3) , CHtml::encode('test3'),true);

					}

					

	

    

    }

  1. Make sure your code is after the line



    <?php echo CHtml::beginForm(); ?>



Hi ,

Now I have another question, if I have a dropdownlist and I wish to populate to more than one

dropdownlist , i.e. MULTIPLE form element , such as dropdownlist2 , input_text1, input_text2 …

how to code in the controller ?

and at the view , the js:‘success’:function(html){jQuery("#city_id").html(html);}

seems that it only take ONE html and fills in its value , so how can I get all the other

form elements value depend on this drop down selection ?

create array for all elements: dropdownlist2 , input_text1, input_text2 …

in Controller:


public function publicarScript()

{

        $jsUrl = Yii::app()->getAssetManager()->publish('protected/controllers/yourFolder/YourClassController.js');

        $cs = Yii::app()->clientScript;

        $cs->registerCoreScript('jquery');

        $cs->registerScriptFile($jsUrl, CClientScript::POS_END);

}


/* 

 * Ajax request

 */

public function actionYourFunctionNameHere() {

...


$arrayDataList = your datalist...

$value_text1 = ; your text1...

$value_text2 = your text2...


... 


$rs['dropdown2'] = $arrayDataList1...

$rs['input1'] = $text_value1;

$rs['input2'] = $text_value2;


echo json_encode($rs);

}

in "YourClassController.js" on the same "yourFolder/YourClassController":


$(document).ready(function() {

    

    // start here

$('#dropDown1').change(function(){

    $.post('index.php?r=yourFolder/YourClassController/YourFunctionNameHere', jQuery(this).parents("form").serialize(), function(arrData){

        eval("var obj="+arrData);


        // populate the dropdown 2

        str = "<option value=''>Select name...</option>";

        $.each(obj.dropdown2, function(i, item){

            str += "<option value="+item.id+">"+item.name+"</option>";

        });

        $('#dropDown2').html(str);

        $('#input1').attr('value',obj.input1);       

        //...

            

    });

});


});

look for my code:

JAVASCRIPT


$(document).ready(function(){

    

    // Popula a dropdown

    $("#serie").change(function(){

        var idSerie = this.value;

        $.post("index.php?r=pedagogico/avaliacao/PreencheDadosForm",{

            idSerie:idSerie

        }, function(dados){            

            eval("var dados="+dados);


            // TURMA // Monta a estrutura de um SELECT

            str = "";

            $.each(dados.turma, function(i, item){

                str += "<option value="+item.id+">"+item.codigo+"</option>";

            });

            $("#turma").html(str);


            // DISCIPLINA // Monta a estrutura de um SELECT

            str = "";

            $.each(dados.disciplina, function(i, item){

                str += "<option value="+item.id+">"+item.codigo+"</option>";

            });

            $("#disciplina").html(str);


            // ETAPA // Monta a estrutura de um SELECT

            str = "";

            $.each(dados.etapa, function(i, item){

                str += "<option value="+item.id+">"+item.codigo+"</option>";

            });

            $("#etapa").html(str);


            // SEGMENTO

            $("#segmento").attr('value',dados.segmento);


        });


    });


});

CONTROLLER ACTION




    /**

     * Carrega as turmas, disciplinas e etapa do segmento e serie

     */

    public function actionPreencheDadosForm($id_serie = null) {

        if($id_serie !== null || isset($_POST['idSerie']) && $_POST['idSerie'] != "") {

            $idSerie = $id_serie !== null ? $id_serie : $_POST['idSerie'];

            $crit = new CDbCriteria();

            $crit->condition = "serie.id = " . $idSerie;

            $serie = serie::model()->with('segmento')->find($crit);


            $dados = array();

            if($serie != null) {

                $dados['turma'] = $this->getTurmaDaSerie($serie);;

                $dados['disciplina'] = $this->getDisciplinaDaSerie($serie);

                $dados['etapa'] = $this->getEtapaDoSegmento($serie);

                $dados['segmento'] = $serie->segmento->id;

            }


            if($id_serie == null)

                echo json_encode($dados);

            else

                return $dados;

        } else {

            $serie = null;

            $dados['turma'] = $this->getTurmaDaSerie($serie);;

            $dados['disciplina'] = $this->getDisciplinaDaSerie($serie);

            $dados['etapa'] = $this->getEtapaDoSegmento($serie);

            $dados['segmento'] = 0;

            echo json_encode($dados);

        }


    }