Drop Down List Based On Checkbox Value

Hi, i have a check box list, based on the selection of the check box want to display different drop down list, and the values are fetch from the controller. My view is,


<?php

    echo $form->radiobuttonList($model,	'category',

      AdminLookup::items('NewsCategory'),

      array('separator'=>'',

         // 'onclick'=>'selectSubGroup',

         'onchange'=>'javascript:selectSubGroup(this.value);',

         'template'=>'<div style=" float:left;

		width:110px;

		font-size:11px; ">{input}&nbsp;{label}</div>'

?>

Drop Down List




<div id="industryDiv" style="display: none;">

<?php

echo $form->dropDownList($model, "sub_category",

    // IndustryLookup::items(),

    array(

	// 'prompt'=>'Choose an Industry',

	'multiple'=>'multiple', 'class'=>'txt-drop-list',

	'style'=>'width: 200px;font-size:12.5px;',));

?>

</div>



This is my script: selectSubGroup


function selectSubGroup(value) {

    var group = value;

	if(group==1) {

		$('#industryDiv').toggle();

		$.ajax({

			type: 'POST', //type Post

			url: '<?php echo Yii::app()->createUrl("//news/Sub"); ?>',

			// data:{result:group},

			success:function(data) {

				alert(data);

				// var resultObj = $.parseJSON(data);

				$('#News_sub_category').val(data);

			 },

			error: function(data) { // if error occured

				alert("Error occured...!")

			},

			dataType:'html' // return html type

		});

	}

	else if(group==2) {

		// other action

	}

	else if(group==3) {

		// other action

	}

	else {

		alert("There is no value;")

	}

}

Controller action is:


public function actionSub(){

	$sub_category = array();

	$info = IndustryLookup::model()->findAll();

	foreach ($info as $value) {

		$sub_category = $value->type;

		// var_dump($sub_category);

		echo CJSON::encode($sub_category);

	}

}

When am trying this i’m getting the values in the data on success. but is not applied to the drop down list.

When i’m trying as below method, after parseJSON i’m getting only the object object as result.


public function actionSub(){

	$info = array();

	$info['sub_group'] = IndustryLookup::model()->findAll();

	echo CJSON::encode($info);

}

[html]$.ajax({

type: 'POST', //type Post


url: '&lt;?php echo Yii::app()-&gt;createUrl(&quot;//news/Sub&quot;); ?&gt;',


// data:{result:group},


success:function(data) { // if success


	alert(data);


	var resultObj = &#036;.parseJSON(data); // getting result obj and assign to new variable


	alert(resultObj.sub_group);


	// alert(resultObj.model);


	 &#036;('#News_sub_category').val(data);


 },


error: function(data) { // if error occured


	alert(&quot;Error occured...&#33;&quot;)


},


dataType:'html' // return html type

});[/html]

Please any one help me, how to assign values to the dropdown list… :(

Hi selvam,

Check the data type while requesting for ajax




$.ajax({

    	type: 'POST', //type Post

    	url: '<?php echo Yii::app()->createUrl("//news/Sub"); ?>',

    	// data:{result:group},

    	success:function(data) { // if success

            	alert(data);

            	var resultObj = $.parseJSON(data); // getting result obj and assign to new variable

            	alert(resultObj.sub_group);

            	// alert(resultObj.model);

             	$('#News_sub_category').val(data);

     	},

    	error: function(data) { // if error occured

            	alert("Error occured...!")

    	},

    	dataType:'json' // return type is json so change to json or comment this line.

});



Also keep tracking the followings

If the above code does’t work try the following in success block




$("#News_sub_category").append(function() {

	return $.map(data, function(el, i) {

    	return '<option value=' + el.val + '>' + el.text + '</option>'; 

	});

});