select with jQuery.ajax don't work.

hi,

i have two select but don’t work together.

here my code:

in view i have




			

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);


			}


  	  }

}



id_cities and city are fields of the my table.

the model class for table "cities" is ok.

any idea?

help me

thanks

The code seems OK… you need to check that you get the proper value in actionDynamiccitie… and with a tool like FireBug you can check the output…

I try to add var_dump ($ _POST) in my controller but does not work. :blink:

how i get country_id ?

:-[

seems that the flow does not reach the controller.

Do not use value as the result.

You need get selected value as




'data'=>array('country_id'=>'js:this.selectedIndex'),



sorry but don’t work. :-[

in my opinion the flow not arrive to controller.

????

Check this thread if it can help you… on comment #18 it has a working project with the depending dropdown example…

sorry what’s thread?

ops… forgot to paste it… here it is - http://www.yiiframework.com/forum/index.php?/topic/14162-

sorry friend,

the code at the link don’t work.

not populate the second select.

I do not know what to do

i finded other way

in my view





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

			echo $form->dropDownList($model, 'id_compagnia', $listacompagnie, 

		 								array

		 									(

		 									'empty' => 'Lista Compagnie',

		 								

		 								 'OnChange'=>'mostraInfo(this.value);'

		 								

		 									)); 

		 ?>



and in view




<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;

}

my suppjquery is :




<?php

$q=$_GET["q"];


$con = mysql_connect('localhost', 'root', 'password');

mysql_select_db("trattamentodati", $con);


# $sql="SELECT * FROM dipendenti WHERE id = '".$q."'";

#$sql= "SELECT `id_ufficio`,`ufficio`, `id_compagnie` FROM `uffici` WHERE id_compagnie = '".$q."'";

$sql= "SELECT `id_ufficio`,`ufficio`, `id_compagnie` FROM `uffici` WHERE id_compagnie = '";

$result = mysql_query($sql);


#echo "<table border='1'>

#<tr>

#<th>Ufficio</th>

#<th>id_compagnia</th>

echo "<strong>Uffici</strong> : <br>";

#</tr>";

echo "<select name='Responsabili[id_uffici]' id='Responsabili_id_uffici' >";

while($row = mysql_fetch_array($result))

{

#echo "<tr>";

#echo "<td>" . $row['ufficio'] . "</td>";

#echo "<td>" . $row['id_compagnie'] . "</td>";


#echo "</tr>";


echo "<option value=".$row['id_ufficio'].">".$row['ufficio']."</option>";


}

#echo "</table>";

echo "</select>";


#echo "<INPUT TYPE=text NAME='cp' SIZE=5 MAXLENGTH=5 value=".$q.">";


mysql_close($con);

?>




this code produces a select : [code]

<select name='Responsabili[id_uffici]' id='Responsabili_id_uffici' >



can i save the result of the new select?

help me!!! :blink:

[/code]

[/code]

;D :lol: :lol: :lol:

ok now everything works.

my view is :




<?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());

?>






my controller is:




public function actionDynamiccities()	

	{

		

#			$criteria = new CDbCriteria();

#			$criteria->select='id, id_cities, city';

#			#$criteria->order='compagnia';

#			$criteria->condition='id_cities = '.$_POST['country_id'];

#			$tipodati=Cities::model()->findAll($criteria);			

#			$data = CHtml::listData($tipodati, 'id', 'city');




			$data=Cities::model()->findAll('id_cities=:id_cities', 

           	array(':id_cities'=>((int) $_POST['country_id'])));

		    $data=CHtml::listData($data,'id','city');

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

		    {

			 echo CHtml::tag('option',array(

			 								'value'=>$value),

			 								CHtml::encode($name),true);

			}

  	  }