You are viewing revision #8 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.
Sometimes new Yii guys face problem to manage dependent dropDownList using AJAX. I am going to discuss an easy solution about this issue.
Example code:
Code in View-
<?php
echo CHtml::dropDownList('region_id','',
array(2=>'New England',1=>'Middle Atlantic',3=>'East North Central'),
array(
'prompt'=>'Select Region',
'ajax' => array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('YourController/loadcities'), //or $this->createUrl('loadcities') if '$this' extends CController
'update'=>'#city_name', //or 'success' => 'function(data){...handle the data in the way you want...}',
'data'=>array('region_id'=>'js:this.value'),
)));
echo CHtml::dropDownList('city_name','', array(), array('prompt'=>'Select City'));
?>
Code in Controller-
public function actionLoadcities()
{
$data=RegionCity::model()->findAll('region_id=:region_id',
array(':region_id'=>(int) $_POST['region_id']));
$data=CHtml::listData($data,'id','city_name');
echo "<option value=''>Select City</option>";
foreach($data as $value=>$city_name)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($city_name),true);
}
I think this will help to understand actually how AJAX works in Yii for dependent dropDownList also AJAX working behavior in Yii framework.
--Enjoy, Explore... Yii
thank
nice tutorial
thanks
It is working fine for display part, I was able to get the first down values using $model but how to save dependent drop down value to $model?
Dependent drop down
I've three drop down.
View Part for the Districts:
<div class="row"> <?php echo $form->labelEx($model,'inst_province'); ?> <?php echo CHtml::dropDownList('inst_province','', array(1=>'A',2=>'B',3=>'C', 4=>'D'), array( 'prompt'=>'Select Province', 'ajax' => array( 'type'=>'POST', 'url'=>CController::createUrl('loaddistricts'), 'update'=>'#inst_distt', 'data'=>array('inst_province'=>'js:this.value'), ))); ?> <span class="help-inline"><?php echo $form->error($model,'inst_province'); ?></span> </div>
<div class="row"> <?php echo $form->labelEx($model,'inst_distt'); ?> <?php echo CHtml::dropDownList('inst_distt','', array(), array('prompt'=>'Select Option'), array( 'ajax' => array( 'type'=>'POST', 'url'=>CController::createUrl('loadtehsils'), 'update'=>'#inst_tehsil', 'data'=>array('inst_distt'=>'js:this.value'), )));?> <span class="help-inline"><?php echo $form->error($model,'inst_distt'); ?></span> </div>
Any advice?
getting error undefined index : region_id
i used the same coding as given above, im getting error undefined index : region_id, help needed
parent dropdownlist from model
hi,
this is the independant dropdown list case, is there an article for dropdownlist from model which get the parent and the child values from the parent?
i need a detailed example, i am new in using yii
thank you
Not saving values to db
Did you ever figure out what the problem was from Your own comment on the orginal topic / tip.. This is the same exact thing aka repost with no solution.
data is coming in database
nice tutorial... its work for me...
but data is not inserting inside database (only country_id and all, rest all are going inside database).
cascade Dropdown list one child depend upon multiple perent
this example is applicable in case of one child depends upon multiple parent?
i have cycle, type and behavior, to select the behavior i have to select the cycle and the type.
can anyone help me in this? i didnt find such issue in the forum
thank you
Gracias
Fue de mucha ayuda!
400 The CSRF token could not be verified
Wondering about the error Yii Error 400 The CSRF token could not be verified...
After some research it turned out that I had to change
'data'=>array('region_id'=>'js:this.value'),
into
'data' => array('region_id' => 'js:this.value', 'YII_CSRF_TOKEN' => Yii::app()->request->csrfToken),
Resolve
@Adil Shahzad
<?php echo $form->error($model,'inst_distt'); ?>
change the class in there to id e.g
<?php echo $form->error($model,'inst_distt'); ?>
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.