controller code
<?php
class PlaceController extends Controller
{
public $layout='//layouts/column1';
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
);
}
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('loadcities','index'),
'users'=>array('*'),
)
);
}
public function actionLoadcities()
{
$data=Place::model()->findAll('state_id=:state_id', array(':state_id'=>(int) $_POST['state_id']));
$data=CHtml::listData($data,'city_id','city');
echo "<option value=''>Select City</option>";
foreach($data as $value=>$city)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($city),true);
}
public function actionIndex()
{
$this->render('index');
}
public function loadModel($id)
{
$model=Place::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
/**
* Performs the AJAX validation.
* @param User $model the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='place-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
?>
model code
<?php
class Place extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'state';
}
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('state,state_id', 'required'),
array('state,state_id', 'length', 'max'=>20),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('state,state_id', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'state' => 'State',
'state_id' => 'State Id'
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('state_id',$this->state_id);
$criteria->compare('state',$this->state,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
?>
view Code
<div class="form">
<?php $form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'place-form',
'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,'state_id'); ?>
<?php echo $form->dropDownList($model,'state_id',
array(1=>‘tamilnadu’,2=>‘karnataka’),
array(
'prompt'=>'Select Region',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('loadcities'),
'update'=>'#city',
‘data’=>array(‘state_id’=>‘js:this.value’),
)));
echo CHtml::dropDownList(‘city’,’’, array(), array(‘prompt’=>‘Select City’)); ?>
<?php echo $form->error($model,'state_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div>