Well,
At first, i read various topics about this case, but none help me to resolve my problem.
I’ll talk about the my situation.
I have 3 tables: anunciantes, cidades, estados.
Anunciantes is a person, the user of site. In his register, the user will fill his cidades (city), and estados (state).
So, in the register form, he’s choose a state, than the dropDownList of cidades, have to be fed with the cityes (cidades), of that State (Estados).
Well, hope that i have been clear.
Now, i’ll show you my code.
The form’s part that have the dropDownLists
<div class="row">
<?php echo $form->labelEx($model,'cod_estado'); ?>
<!-- Dropdown de Estados com chamada Ajax para atualizar dropDown de cidades -->
<?php echo $form->dropDownList($mestados,'cod_estado',CHtml::listData($mestados->getAllEstados()->queryAll(), 'cod_estado', 'desc_estado'),
array(
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('Anunciantes/getCidadesByEstado'),
'update'=>'#'.CHtml::activeId($mcidades,'cod_cidade'),
)));
?>
<!-- End dropDown Estados -->
<?php echo $form->error($model,'cod_estado'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'cod_cidade'); ?>
<?php echo $form->dropDownList($mcidades,'cod_cidade', array()); ?>
<?php echo $form->error($model,'cod_cidade'); ?>
</div>
My actions create and getCidadesByEstado, and the accessRules
public function actionCadastro() #Action Create
{
$model = new Anunciantes;
$mcidades = new Cidades();
$mestados = new Estados();
if (isset ($_POST['Anunciantes'])) {
$model->atributes = $_POST['Anunciantes'];
$model->cod_cidade = $_POST['Cidades']['cod_cidade'];
$model->cod_estado = $_POST['Estados']['cod_estado'];
if($model->save())
$this->redirect(array());
}
$this->render('Cadastro', array(
'model'=>$model,
'mcidades'=>$mcidades,
'mestados'=>$mestados,
));
}
# Atualização dinamica de dropDown Cidade
public function actiongetCidadesByEstado()
{
echo "teste";exit;
$mcidades = new Cidades();
$mcidades = Cidades::model()->findAll('cod_estado=:cod_estado',
array(':cod_estado'=>(int)$_POST['Estados']['cod_estado']));
$mcidades = CHtml::listData($mcidades, 'cod_cidade', 'desc_cidade');
foreach($mcidades as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('cadastro','getCidadesbyEstado'),
'users'=>array('*'),
),
);
}
Before i forget, i used a echo "teste"; to see if the action is called correctly, but
the echo is not performed, although i don’t find any error in code.
Well, somebody knows what can by my problem?
Abraço