More information about this topic
Reset database  | 
Dependent Listbox example
  

protected/modules/FormsModule/controllers/FormsController.php

	public function actionDependentlistbox() {
		$model=new Dblist;
		$this->render('dependentlistbox', array('model'=>$model));

	//	$this->render('dependentlistbox');
	}

protected/modules/FormsModule/models/Dblist.php

<?php

class Dblist extends CFormModel
{
	public $province_group;
	public $province;		
	public	$province_city;

	
	public function rules()
	{
		return array(
			
		);
	}
		
	public function getdata()
	{
			$qtxt ="SELECT province_group,province_group_desc FROM province_group ORDER BY province_group_sorting ASC";
			$command =Yii::app()->db->createCommand($qtxt);
		//	$command->bindValue(":username", '%'.$_GET['term'].'%', PDO::PARAM_STR);
			$rows=$command->queryAll();
			return $rows;
	}

}

rotected/modules/FormsModule/views/forms/dependentlistbox.php


	$form=$this->beginWidget('CActiveForm', array(
							'id'=>'Demo-Form',
							'enableClientValidation'=>true,
							'enableAjaxValidation' => true,
							'clientOptions'=>array(
							'validateOnSubmit'=>false,
						),
				)); 
?>
<table>
						<tr>
							<td>
<?php 								
					// $a_data	=	CHtml::listData(Dblist::getdata(), 'province_group','province_group_desc');
					$a_data	=	Dblist::getdata();
					foreach($a_data as $v) {
						$a[$v['province_group']] =	$v['province_group_desc'];
					}
					echo $form->dropDownList($model,'province_group',$a,   
								array(
									'prompt' => '- - - Select - - -',
		                            'ajax' => array(
		                         			   'type' => 'GET',
		                            			'url' => CController::createUrl('/FormsModule/forms/dependentlistbox_p'),
		                            			'update' => '#Dblist_province',
												'data'=>array('province_group'=>'js:this.value'),
		                          			),
		                          	'onchange'=>'js:$("#Dblist_province").focus()',
		                         )
                          ); ?>
							</td>
                            <td>
					<?php
					$a_data_selectmodel	=	array();
					echo $form->dropDownList($model,'province',$a_data_selectmodel,
								array(
									'prompt' => '- - - Select Province - - -',
		                            'ajax' => array(
		                         			   'type' => 'GET',
		                            			'url' => CController::createUrl('/FormsModule/forms/dependentlistbox_city'),
		                            			'update' => '#Dblist_province_city',
												'data'=>array('province_group'=>'js:$("#Dblist_province_group").val()',
															 			 'province'=>'js:this.value'),
												'onchange'=>'js:$("#Dblist_province_city").focus()',
		                          			)
		                         )
					); ?>			
					</td><td>
					<?php
						$a_data_selectno	=	array();
						echo $form->dropDownList($model,'province_city',$a_data_selectno,array(
									'prompt' => '- - - - - Select City - - - - - - ',)); 
					?>
					</td>
					<td>
                    	&nbsp;&nbsp;
					</td>
					</tr></table>