[Solved] Dependent Dropdownlist Not Working (Ajax)

I have two dropdownlists: User and Projects. While choosing a User, I want to show only those projects that are attached to that selected user, but it is not working. This is my view:

    <?php $form=$this->beginWidget('CActiveForm', array(


        // Please note: When you enable ajax validation, make sure the corresponding

        // controller action is handling ajax validation correctly.

        // See class documentation of CActiveForm for details on this,

        // you need to use the performAjaxValidation()-method described there.


    )); ?>

<p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

<div class="row">

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

        <?php echo CHtml::dropDownList('user_id', '', CHtml::listData($model->getAllUsers(), 'id', 'username'), array(

            'prompt'=>'Select User',

            'ajax' => array(



                'update'=>'#'. CHtml::activeId($model, 'project_id'),


            )));  ?>

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


    <div class="row">

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

        <?php echo CHtml::dropDownList('project_id','', array(), array('prompt'=>'Select Project')); ?>

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


This is the action:

public function actionLoadProjects() {


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


        echo "<option value=''>Select Project</option>";

        foreach($data as $value=>$project_id)

            echo CHtml::tag('option', array('value'=>$project_id), Project::model()->getProjectName($project_id),true);


Any ideas?

May the extension cascadedropdown does the job.

Do you mean that this and this are irrelevant?

Try to specify what’s going wrong. Does your browser’s developer console show any errors at page load or when you change the drop down? Does it show that a request is being sent to the server? Does it show a valid response?

The more information you provide, the easier it is for people to spot the error. The problem might become apparent for you just as a result of that preliminary debugging.

Ok. The problem is that when i choose any user from the User DropDownList, nothing happens on the other DropDownList, it just keep showing the prompt "Select Project" and nothing else.

This is what’s in JS console:

I’ve just added this post to the posting guidelines. Please go through each step and post the results here.

The warning that you’ve shown shouldn’t cause any problems, so let us know if you encounter any others.

It’s not irrelevant, but maybe it’s more easy/reusable to implement with the extension.

Fine. I’ve done that and I see the response is:

[html]<option value=’’>Select Project</option><option value=“3”>Some new project</option>[/html]

So it is getting the right result, the problem is that it’s not refreshing the ‘select’ Project list.

You’re creating the form field with the CHtml helper instead of the CActiveForm object you created:

<?php echo CHtml::dropDownList('project_id','', array(), array('prompt'=>'Select Project')); ?>

That means that the drop down list won’t receive the ID that you specified here:

'update'=>'#'. CHtml::activeId($model, 'project_id'),

Try to use the $form->dropDownList() method to generate your form field.

I get a terrible:

[color="#FF0000"][size="2"]Trying to get property of non-object[/size][/color]

Changing it to $model:

<?php echo $form->dropDownList($model, '', CHtml::listData($model->getAllUsers(), 'id', 'username'), array

I get:

[color="#FF0000"][size="2"]Property "UserProjJunction." is not defined.[/size][/color]

Can you show the full details of the error? Stack trace as well if possible.

Solved. After changing it to a $model var, instead of ‘user_id’ (ID, that’s what it says at the wiki articles), i got the Property “UserProjJunction.” is not defined. error, because it does not recognize the ID field since it is like:

<?php echo $form->dropDownList($model, ''

So the solution was this:

echo $form->dropDownList($model, 'user_id'...

echo $form->dropDownList($model,'project_id'...

Thanks for helping!!


The only problem is that it shows only the first project attached to a user, and not the full list of projects?? So how can i do it to print all the projects??