Need help with AJAX and JSON

I’ve been struggling with this problem for a week now. I’m trying to update a dropdownlist dependant on another dropdownlist. Now I have studied the guides about dependant dropdowns and how to update fields with ajax and have the gist of it working.

I get the second dropdown to fill in the textfields as I want but for some reason the first dropdown doesn’t update the second dropdown. I’ve also tried to change the second dropdown to a textfield where I tried to display the value from the first dropdown but that didn’t work which leads me to believe that the first dropdown doesn’t call the json function as it should.

Here’s the code for the form and controller:


	

	<div class="row">

		<?php //echo $form->labelEx($model,'productId'); ?>

		<?php echo $form->dropDownList($model,'category', Productcategories::model()->getAllProductcategories(), array(

						'onChange'=>"{productList();}"

		)); ?>

	</div>

	

	<div class="row">

		<?php echo $form->labelEx($model,'productId'); ?>

		<?php echo $form->dropDownList($model,'productId', array(), array(

						'onChange'=>"{selectProduct();}"

		)); ?>

		<?php echo $form->error($model,'productId'); ?>

	</div>

	

Script:


<script type="text/javascript">

	function productList()

	{

		<?php echo CHtml::ajax(array(

			'url'=>CController::createUrl('productionorders/productList'),

			'data'=>array('category'=>'js:$(\'#Productionorders_category\').val()'),

			'type'=>'POST',

			'dataType'=>'json',

			'success'=>"function(data)

			{

				$('#Productionorders_productId').append(data.list);

			} ",

		)); ?>


		return false;

	}

	

	function selectProduct()

	{

		<?php echo CHtml::ajax(array(

			'url'=>CController::createUrl('productionorders/selectProduct'),

			'data'=>array('productId'=>'js:$(\'#Productionorders_productId\').val()'),

			'type'=>'POST',

			'dataType'=>'json',

			'success'=>"function(data)

			{

				$('#Productionorders_materialtypeId').val(data.materialtypeId);

				$('#Productionorders_materialtypedisplay').val(data.materialtype);

				$('#Productionorders_length').val(data.length);

			} ",

		)); ?>


		return false;

	}

	

</script>

Controller:


public function actionProductList()

	{

		if(Yii::app()->request->isAjaxRequest)

		{

			$categoryId = Yii::app()->request->getParam('category');

			

			$criteria = new CDbCriteria();

			$criteria->select = 'Id, name';

			$criteria->condition = 'productcategoryId = :Id';

			$criteria->params = array(':Id'=>$categoryId);

			

			$products = Products::model()->findAll($criteria);

			

			foreach($products as $value=>$name)

			{

				$productList .= CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);

			}

			

			echo JSON::encode(array(

				'error'=>'false',

				'list'=>$productList,

			));

			

			Yii::app()->end();

		}

	}

	

	public function actionSelectProduct()

	{

		if(Yii::app()->request->isAjaxRequest)

		{

			$productId = Yii::app()->request->getParam('productId');

			

			$product = Products::model()->findByPk($productId);

			

			echo CJSON::encode(array(

				'error'=>'false',

				'materialtypeId'=>$product->materialtypeId,

				'materialtype'=>$product->materialtype->materialtype,

				'length'=>$product->length,

			));

			

			Yii::app()->end();

		}

	}

This is my first go at ajax and json so any help would be appriciated

edit: also the first drop isn’t actually part of this model but I added the category attribute to the model so I could use $form->dropDownList rather than CHtml::dropDownList.

is the ajax request beeing sent ?

try




'onChange'=>"js:productList();"

Unfortunetly that didn’t work either.

is the ajax request beeing sent ?

Found the problem. I had JSON::encode rather than CJSON::encode in the controller. Can’t believe I missed it since I’ve checked the code 5-6 times for spelling errors. I only found it because I started using firebug which I hadn’t heard about until 2 days ago <_<