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(
'id'=>'user-proj-junction-addUserToProject-form',
// 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.
'enableAjaxValidation'=>false,
)); ?>
<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(
'type'=>'POST',
'url'=>array('/project/loadProjects'),
'update'=>'#'. CHtml::activeId($model, 'project_id'),
'data'=>array('user_id'=>'js:this.value'),
))); ?>
<?php echo $form->error($model,'user_id'); ?>
</div>
<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'); ?>
</div>
This is the action:
public function actionLoadProjects() {
$data=UserProjJunction::model()->findAll('user_id=:user_id',
array(':user_id'=>(int) $_POST['user_id']));
$data=CHtml::listData($data,'id','project_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);
}
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.
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:
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??