Skip to content

Instantly share code, notes, and snippets.

@pebriana
Created June 2, 2016 03:38
Show Gist options
  • Save pebriana/f4a27eca9c5527ac9d319a3fdf6e1120 to your computer and use it in GitHub Desktop.
Save pebriana/f4a27eca9c5527ac9d319a3fdf6e1120 to your computer and use it in GitHub Desktop.
how to change values in a dropdown list using ajax in Yii
public function actionMysubcategory(){
$model = new YourModel();
$cid = Yii::app()->request->getPost('cid');
$subcategory= Subcategory::model()->findAll(
array('order'=>'sid',
'condition'=>'cid=:cid',
'params'=>array(':cid'=>$cid)));
$list = CHtml::listData($subcategory,
'sid', 'sname');
echo CHtml::activeDropDownList($model,'sid',$list);
}
<?php
$list = CHtml::listData($category,
'cid', 'cname');
echo $form->dropDownList($model,'cid',$list,
array('empty' => '(Select a category)'));
?>
<label>Sub Category *</label>
<div id="sub-category-wrapper">
<?php
$subcategory = array();
echo $form->dropDownList($model,'sid',$subcategory,
array('empty' => '(Select a subcategory)'));
?>
</div>
<?php
Yii::app()->clientScript->registerScript('stepjavascript', '
$("#Product_cid").change(function (){
var cid = $("#Product_cid").val();
var path = "'.$this->createUrl('gotoController/mysubcategory').'";
$.ajax({
type: "POST",
url: path, //url to be called
data: { cid: cid }, //data to be send
success: function( response ) {
$("#sub-category-wrapper").html(response);
}
})
});
', CClientScript::POS_END);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment