Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php echo $form->labelEx($model,'country'); ?>
- <?php $list=CHtml::listData($country,'id', 'country'); ?>
- <?php echo CHtml::dropDownList('country_id','',$list,array(
- 'ajax'=>array(
- 'type'=>'POST',
- 'url'=>CController::createUrl('test/states'),
- 'update'=>'#stateselect',
- 'dataType'=>'json',
- 'success'=>'js:function(data){
- jQuery("#stateselect").removeAttr("disabled");
- jQuery("#country_id").append(data);
- }',
- )
- )
- );?>
- <?php echo $form->error($model,'country_id'); ?>
- </div>
- <!--ajax state div-->
- <?php
- echo CHtml::dropDownList('stateselect','', array());
- ?>
- <!--end of ajax div -->
- public function actionStates()
- {
- $state=State::model()->findAll('country_id=:country_id',
- array(':country_id'=>(int) $_POST['country_id']));
- $country=Country::model()->findAll();
- foreach($state as $value=>$name)
- {
- $stateselect .= CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
- }
- foreach($country as $value=>$name)
- {
- $country_id .= CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
- }
- // return data (JSON formatted)
- echo CJSON::encode(array(
- 'stateselect'=>$stateselect,
- 'country_id'=>$country_id
- ));
- }
Add Comment
Please, Sign In to add comment