Hi, guys. First post. Very newbie and non native english speaker, please be patient.
I’m trying to implement the classical dependable dropdown solution from [url="http://www.yiiframework.com
/wiki/24/"]http://www.yiiframework.com/wiki/24/[/url] but the ajax request is not being triggered.
In summary, I want a dropdown populated with categories and when a category is selected the products dropdown is populated with the corresponding products via ajax.
Here is the code:
<?php
//table Product: columns id,categoryId,description
//table ProductCategory: columns id,description
$product=new Product();
$productCategory=new Productcategory();
$form=$this->beginWidget('CActiveForm');
echo $form->dropDownList($productCategory,'id',
CHtml::listData($productCategory::model()->findAll(),'id','description'),
array('ajax'=>array(
'url'=>CController::createUrl('market/dynamicProducts'),
'update'=>'#'.CHtml::activeId($product,'id')
)
)
);
echo $form->dropDownList($product,'id',array());
$this->endWidget();
?>
Checking Firebug, no request is generated. The rendered page HTML is as follows:
<form id="yw0" action="/mm/index.php/market/product" method="post">
<select name="Productcategory[id]" id="Productcategory_id">
<option value="1">Steel products</option>
<option value="2">Wood products</option>
<option value="3">Fasteners</option>
<option value="4">Bearings</option>
<option value="10">Furniture</option>
<option value="11">Bedding</option>
</select>
<select name="Product[id]" id="Product_id">
</select>
</form>
Yii didn’t generate any Javascript code to handle the ajax request. I’ve tried to force the script generation adding the following code:
<?php
Yii::app()->clientScript->registerScript('dropdown',
"$(".CHtml::activeId($productCategory,'id').").change(function(){"
. "jQuery.ajax({'url':'".CController::createUrl('market/dynamicProducts')."',"
. "'cache':false,"
. "'success':function(html){"
. "jQuery(".CHtml::activeId($product,'id').").html(html);}"
."});"
."return false;"
."});"
);
?>
Again, no Javascript generated and the HTML output is identical to the previous attempt.
The only solution I found was to add the <script> tag manually:
<script src="/mm/assets/2d1fd728/jquery.js" type="text/javascript"></script>
<script>
$(Productcategory_id).change(function(){
jQuery.ajax({'url':'/mm/index.php/market/dynamicProducts',
'data':jQuery(this).serialize(),
'cache':false,
'type':'POST',
'success':function(html){
jQuery(Product_id).html(html);
}
}
);
return false;
}
);
</script>
It is strange that 2 different methods failed to generate the Javascript code. Even more strange is that when I use CHtml::ajaxLink the Javascript code is generated.
Is there any Yii setting or server configuration that may be blocking Javascript generation?