Hi guys,
It is regarding the dependent dropdown functionality I am looking to achieve in my web application. I have spent so many hours trying different things but not able to solve the problem. I would appreciate your help in this regard.
MYSQL Tables (under discussion): MainProfileTable, Cities
MainProfileTable Model Name: MainProfileTable.php
MainProfileTable controller Name: MainProfileTableController.php
Cities Model Name: Cities.php
Following is the code in MainProfileTableController.php
public function actionCities()
{
$cities = Cities::model()->findAll('country_name=:c_name',array(':c_name'=>$_POST['Country']));
$data = CHtml::listData( $cities, 'country_name', 'city_name' );
foreach ( $data as $value => $name )
{
echo CHtml::tag( 'option', array( 'value' => $value ), CHtml::encode( $name ), true );
}
}
Following is the code in _form.php file of the MainProfileTable views:
////////////////// this code is at the begining of the file ////////////////////
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'main-profile-table-form',
'enableAjaxValidation'=>false,
)); ?>
/////////////////////////////////////////////////////////////////////////////////
//then this is the code under discussion:
<div class="row">
<?php echo $form->labelEx($model,'Country'); ?>
<?php echo $form->dropDownList($model,'Country',MainProfileTable::getCountriesDBOptions(),array('style'=>'width:300px'),array('ajax'=>array('type'=>'GET',
'url'=>CController::createUrl('mainProfileTable/cities'),
'update'=>'#City'))); ?>
<?php echo $form->error($model,'Country'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'City'); ?>
<?php echo $form->dropDownList($model,'City',array(),array('style'=>'width:300px')); ?>
<?php echo $form->error($model,'City'); ?>
</div>
Following is the access control in controller file:
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','cities'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','cities'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
That is all I have been working on. Please figure out where the problem lies. It does not give any error as such but do not display any values.
I also tried populating data in actionCities() function manually/static. Even then it loads nothing.
Awaiting quick response.
Regards