Yii Framework Forum: dependent dropdown list - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

dependent dropdown list Rate Topic: -----

#1 User is offline   parmesh 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 10-June 12
  • Location:pune

Posted 11 June 2012 - 08:13 AM

for this
i have two tables one is registaration having many columns.
and second table is citylist in which there are only three columns city_id,city_name,state name

and i want to popualte dropdown for city list by getting state name from table and insert into
first table i.e registration


this is my view.php
<div class="row">
<?php echo $form->labelEx($model,'State'); ?>
<?php
$list=CHtml::listData(tblcitylist::model()->findAll(),'state','state');
echo CHtml::activeDropDownList('prjid','', $list,
array(
'empty'=>'Please Select',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Registrationd/dynamiccity'), //url to call.
'update'=>'#prjid', //selector to update
'data'=>'js:$(this).serialize()',

)));
?>
<?php echo $form->error($model,'State'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'City'); ?>
<?php
//empty since it will be filled by the other dropdown
echo CHtml::activeDropDownList('prjid','', array(),array('empty'=>'Select','style'=>'width:120px;'));
//echo CHtml::activeDropDownList($model,'city_id', array(),array('prjid'=>'city_id'));
// echo CHtml::activeHiddenField($model,'city_id');
?>
<?php echo $form->error($model,'City'); ?>
</div>



and then in controller.php
public function actiondynamiccity()
{
$data=Lab::model()->findAllBySql('select city_name from tblcitylist where state='.$_POST['state']);
$data=CHtml::listData($data,'prjid','city_name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}

}
0

#2 User is offline   Rajith R 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 20-April 11
  • Location:Kochi , Kerala, India

Posted 11 June 2012 - 08:25 AM

use 2nd like this, give that id to it..

echo CHtml::activeDropDownList($model,'batch_id',$data1,array('prompt'=>'Select','id'=>'batch_id'));



use another id to 2nd dropdown
'update'=>'#batch_id'
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#3 User is offline   Rajith R 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 20-April 11
  • Location:Kochi , Kerala, India

Posted 11 June 2012 - 08:27 AM

echo CHtml::activeDropDownList($model,'city_id', array(),array('id'=>'city_id'));


and update 'city_id'
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#4 User is offline   parmesh 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 10-June 12
  • Location:pune

Posted 12 June 2012 - 02:03 AM

View PostRajith R, on 11 June 2012 - 08:25 AM, said:

use 2nd like this, give that id to it..

echo CHtml::activeDropDownList($model,'batch_id',$data1,array('prompt'=>'Select','id'=>'batch_id'));



use another id to 2nd dropdown
'update'=>'#batch_id'


if possible please send me modified code,
i am not getting you....
actually i am trying to populate both state and city drop down list from same table.
0

#5 User is offline   ehochedez 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 17-November 11
  • Location:China

Posted 12 June 2012 - 03:35 AM

Hi,

don't forget to use the code snippet tool when you insert code in your forum post, otherwise it's not easy to read your code.
You should have a look at this if you haven't yet, there are complete examples:

http://www.yiiframework.com/wiki/24/

Hope it helps.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users