echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('responsabili/dynamiccities'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city_id', //selector to update
//'data'=>'js:javascript statement'
'data'=>array('country_id'=>'js:this.value'),
//leave out the data key to pass all form values through
)));
echo CHtml::dropDownList('city_id','', array());
my contreller is:
public function actionDynamiccities($model)
{
$data=Cities::model()->findAll('parent_id=:parent_id',
array(':parent_id'=>(int) $_POST['country_id']));
$data=CHtml::listData($data,'id_cities','city');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
}
<script language="javascript">
var xmlhttp;
function mostraInfo(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="http://127.0.0.1/suppjquery1.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("info").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
<?php
echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
#'url'=>"index.php?r=responsabili/dynamiccities",
'url'=>CController::createUrl('responsabili/dynamiccities'),
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city_id', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
'data'=>array('country_id'=>'js:this.selectedIndex'),
)));
echo CHtml::dropDownList('city_id','', array());
?>