Dependent Dropdown - $Post Array Empty In Controller

Hi folks, I’m trying to set up a dependent dropdown list, in which a department is selected from the first dropdown list, and then the second dropdown list is populated with the courses belonging to that department. I’ve almost got it working, but my $POST array is not receiving the data from the view. Here is my code:

In the view courseTaken/_form.php:


<div class="row">

        <?php

        echo $form->labelEx($model,'department_id');

        echo CHtml::dropDownList('department_id','', CHtml::listData(Department::model()->findAll(array(

                'order'=>'department_name ASC')), 'id', 'department_name'),

                array('ajax'=>array(

                'type='=>'POST',

                'url'=>CController::createUrl('course/CoursesByDept'),

                'update'=>'#course_id',

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

        )));

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

        ?>

</div>


<div class="row">

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

        <?php echo CHtml::dropDownList('course_id', '', array()); ?>

        <?php echo $form->error($model,'course_id'); ?>

</div>

In the controller CourseController:


public function actionCoursesByDept()

{

        $data=Course::model()->findAll('department_id=:department_id',

        array(':department_id'=>(int) $_POST['department_id']));

        //array(':department_id'=>1));

        $data=CHtml::listData($data,'id','course_number');

        foreach($data as $value=>$name) {

                echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);

        }

}



Using Firefox and Firebug, I see the error: Undefined index: department_id

on the line with the $POST array. If I comment out the line with the $POST array, and just manually assign an id:


$data=Course::model()->findAll('department_id=:department_id', array(':department_id'=>1));

the course dropdown in the view gets loaded with all of the courses with department_id = 1.

Does anyone know why the $POST array is empty?

The CourseTaken model does not have the field department_id, the Course model does, so I’ve put:


public $department_id;

inside the CourseTaken model, but I get the same error.

I’ve read the wiki articles http://www.yiiframework.com/wiki/24/ and http://www.yiiframework.com/wiki/429/an-easy-solution-for-dependent-dropdownlist-using-ajax/

and read lots of forum posts, but the problem still eludes me.

Any help appreciated.

Stumped,

Larry

Hi,

Please can you try to bind dropdownlist with current form. I hope it will work…

$model – modelname

pgm_doc – attribute name

$model->printFileNames – listdata

then give your ajax inside array

Example

<?php echo $form->dropDownList($model,‘pgm_doc’, $model>printFileNames,array(‘disabled’=>$action===‘view’,‘id’=>‘filelist’)); ?>

your original code

    echo CHtml::dropDownList('department_id','', CHtml::listData(Department::model()-&gt;findAll(array(


            'order'=&gt;'department_name ASC')), 'id', 'department_name'),


            array('ajax'=&gt;array(


            'type='=&gt;'POST',


            'url'=&gt;CController::createUrl('course/CoursesByDept'),


            'update'=&gt;'#course_id',


            'data'=&gt;array('department_id'=&gt;'js:this.value'),


    )));

Thanks

chandran nepolean

Instead of CHtml::dropDownList you should use CActiveForm::dropDownList:


<div class="row">

        <?php

        echo $form->labelEx($model,'department_id');

        echo $form->dropDownList($model,'department_id', CHtml::listData(Department::model()->findAll(array(

                'order'=>'department_name ASC')), 'id', 'department_name'),

                array('ajax'=>array(

                'type='=>'POST',

                'url'=>CController::createUrl('course/CoursesByDept'),

                'update'=>'#course_id',

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

        )));

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

        ?>

</div>


<div class="row">

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

        <?php echo CHtml::dropDownList('course_id', '', array()); ?>

        <?php echo $form->error($model,'course_id'); ?>

</div>

Yes I tried that, but it does’nt work because there is no department_id in the CourseTaken model ($model in _form.php). The CourseTaken model has a course_id, and the Course model has the department_id. I’m just trying to produce a list of courses that belong to a department that is selected in the first dropdown, when you enter a course that you’ve taken.

Yii produces the error “Invalid argument supplied for foreach()”, in the file yii-1.1.13/framework/web/helpers/CHtml.php. I’ve added department_id as a virtual attribute in the CourseTaken model, but it does’nt help:


class CourseTaken extends CActiveRecord

{

        public $department_id;

Still stumped.

Larry

I got it working using Yii::app()->request->getParam(‘department_id’) in the controller. Using $_POST[‘department_id’] just does not work. Everything else is the same.

Does anybody know why $_POST is blocked in the controller?


        public function actionCoursesByDept()

        {

                $departmentID = Yii::app()->request->getParam('department_id');

                //$departmentID = $_POST['department_id'];

                $data=Course::model()->findAll('department_id=:department_id',

                   array(':department_id'=>(int) $departmentID));

                $data=CHtml::listData($data,'id','course_number');

                foreach($data as $value=>$course) {

                        echo CHtml::tag('option', array('value'=>$value),CHtml::encode($course),true);

                }

        }

So wonderful, So tricky.

What is the difference?

Well the reason it works with


$departmentID = Yii::app()->request->getParam('department_id');

is because getParam() reads $_GET or $_POST.

If I use $_GET instead of $_POST, it works:

In the view:


        <div class="row">

                echo $form->labelEx($model,'department_id');

                echo CHtml::dropDownList('department_id','', CHtml::listData(Department::model()->findAll(array(

                        'order'=>'department_name ASC')), 'id', 'department_name'),

                        array('empty' => 'Select Department',

                        'ajax'=>array(

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

                        'type='=>'GET',

                        'url'=>CController::createUrl('CoursesByDept'),

                        'update'=>'#course_id',

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

                )));

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

                ?>

        </div>


        <div class="row">

                <?php

                echo $form->labelEx($model,'course_id');

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

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

                ?>

        </div>

In the controller:


        public function actionCoursesByDept()

        {

                //$departmentID=(int)$_POST['department_id'];

                //$departmentID = Yii::app()->request->getParam('department_id');

                $departmentID = $_GET['department_id'];


                $data=Course::model()->with(

                        'department')->findAll(array(

                        'condition'=>'department_id=:department_id',

                        'params'=>array(':department_id'=>(int) $departmentID),

                        'order'=>'department.department_abbreviation, course_number ASC'

                ));


                $data=CHtml::listData($data,'id','UniversityDepartments');

                foreach($data as $value=>$course) {

                        echo CHtml::tag('option', array('value'=>$value),CHtml::encode($course),true);

                }

        }

I’ve turned off all accessControl in the controller, and fiddled with the ajax options in the CActiveForm widget, but still the $_POST does’nt make it to the controller.

I would prefer to use $_POST.

Anybody have any other ideas?

Larry

Wow, and when I select a course in the dependent dropdown, it’s not being submitted. It’s a required field in the model but validation fails. The page source looks fine.

Larry