I am trying to implement a dependent dropdown in which it will not retrieve its data from Database, however, i certainly would like to be save their entry in my table.
I have tried to use the tutorial of Yii i.e. http://www.yiiframework.com/wiki/24/
But as you can see that it is retrieving data from table but i want that an array to be populated against the selected value.
public function actionDynamiccities()
{
$data=Location::model()->findAll('parent_id=:parent_id',
array(':parent_id'=>(int) $_POST['country_id']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
Please assist
Javascript could be an option but then how would i implement it in Yii form.
This is my example program from the given link (http://www.yiiframework.com/wiki/24/)
<!-- ===================== My First Dropdown =========== -->
<?php
echo CHtml::dropDownList('country_id', '', array(1 => 'USA', 2 => 'France', 3 => 'Japan'), array(
'ajax' => array(
'type' => 'POST', //My method type
'url' => CController::createUrl('site/CallCities'), //This is my request/ajax URL
array('coountry_id'=>'js:this.value'), //I'm passing the selected dropdonw value.
'dataType' => 'JSON',
'success'=>'js:function(data)' //The functionaliy after success
. '{'
. ' var html="";'
. ' $.each(data,function(i,obj)'
. ' {'
. ' html+="<option value=obj.id>"+obj.name+"</option>"'
. ' });'
. ' $("#city_id").html(html);'
. '}'
)));
?>
<!-- ===================== My Second Dropdown =========== -->
<?php
echo CHtml::dropDownList('city_id', '', array());
?>
And, my controller method is
public function actionCallCities()
{
$cityId=$_POST['coountry_id'];
$criteria=new CDbCriteria();
$criteria->select=array('id,name');
$criteria->condition='city_id='.$cityId;
$criteria->order='name';
$cityAry= MyCity::model()->findAll($criteria);
$ary=array();
foreach($cityAry as $i=>$obj)
{
$ary[$i]['id']=$obj->id;
$ary[$i]['name']=$obj->name;
}
echo json_encode($ary);
}
I hope, this example program will work for you