Hi all!
I’m following this post to create three dropDownLists. The first list is for District. The second is for Precinct. The third is for Street. I’ve tried all the methods in comment section but no lucky. Here is my code in view file:
<?php
/* @var $this Address2Controller */
/* @var $model Address2 */
/* @var $form CActiveForm */
?>
<div class="form">
<?php echo CHtml::beginForm(); ?>
<?php echo CHtml::errorSummary($model); ?>
<div class="row">
<?php echo CHtml::activeLabel($model,'district_id'); ?>
<?php
echo CHtml::dropDownList('district_id','', $model->getDistrictOptions() ,
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('dynamicprecincts'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#precinct_id', //selector to update
'data'=>'js:$(this).serialize()')
)); ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'precinct_id'); ?>
<?php
echo CHtml::dropDownList('precinct_id','', array(),array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('dynamicstreets'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#street_id', //selector to update
'data'=>'js:$(this).serialize()')
));
?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'street_id'); ?>
<?php
echo CHtml::dropDownList('street_id','', array());
?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'address_number'); ?>
<?php echo CHtml::activeTextField($model,'address_number',array('size'=>45,'maxlength'=>45)); ?>
<?php echo CHtml::error($model,'address_number'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->
And here is the code in controller.
public function actionDynamicprecincts()
{
$data = Precinct::model()->findAll('district_id=:parent_id',
array(':parent_id'=>(int) $_POST['district_id']));
$data= CHtml::listData($data,'precinct_id','precinct_name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
public function actionDynamicStreets(){
$data = Street::model()->findAll('precinct_id=:parent_id',
array(':parent_id'=>(int) $_POST['precinct_id']));
$data= CHtml::listData($data,'street_id','street_name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
And a function for data to provide dropDownList.
public function getDistrictOptions()
{
$models = District::model()->findAll();
if($models===null)
{
throw new CHttpException(404,'The requested city does not exist.');
}
$districtArray = CHtml::listData($models, 'district_id', 'district_name');
return $districtArray;
}
The ajax seems to work because the second list is filled out depending on the selected item of the first list. And same thing for third list.
3640
But the data was not saved in data base.
3641
I’ve also tried using
<?php echo CHtml::beginForm(); ?>
<?php echo CHtml::endForm(); ?>
Any help really appreciated!
Regards.