Guest User

Untitled

a guest
Jan 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. <?php echo $form->labelEx($model,'country'); ?>
  2. <?php $list=CHtml::listData($country,'id', 'country'); ?>
  3. <?php echo CHtml::dropDownList('country_id','',$list,array(
  4. 'ajax'=>array(
  5. 'type'=>'POST',
  6. 'url'=>CController::createUrl('test/states'),
  7. 'update'=>'#stateselect',
  8. 'dataType'=>'json',
  9. 'success'=>'js:function(data){
  10. jQuery("#stateselect").removeAttr("disabled");
  11. jQuery("#country_id").append(data);
  12. }',
  13. )
  14. )
  15. );?>
  16. <?php echo $form->error($model,'country_id'); ?>
  17. </div>
  18.  
  19.  
  20. <!--ajax state div-->
  21.  
  22. <?php
  23. echo CHtml::dropDownList('stateselect','', array());
  24. ?>
  25. <!--end of ajax div -->
  26. public function actionStates()
  27. {
  28. $state=State::model()->findAll('country_id=:country_id',
  29. array(':country_id'=>(int) $_POST['country_id']));
  30. $country=Country::model()->findAll();
  31. foreach($state as $value=>$name)
  32. {
  33. $stateselect .= CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
  34. }
  35.  
  36. foreach($country as $value=>$name)
  37. {
  38. $country_id .= CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
  39. }
  40.  
  41. // return data (JSON formatted)
  42. echo CJSON::encode(array(
  43. 'stateselect'=>$stateselect,
  44. 'country_id'=>$country_id
  45. ));
  46. }
Add Comment
Please, Sign In to add comment