Hi everybody,
definitely new to YII (3 days playing with it and happy I’ve been directed to it).
I’ve been reading and searching a lot to make the dependent dropdowns example by dalip work.
It does now, with a few changes to the original article code and a strange problem.
Overview: I manage holiday trip quotes, each having a package id and a voyage id; each quote’s voyage id depends on selected package id (e.g. package id 1 ‘owns’ voyage ids 1, 2 and 4, while package id 2 ‘owns’ voyage ids 3, 5 and 6). Ok, keeping both ids is unnecessary, it only serves for my test code.
In my quotes view I have (see code comments for changes to the original dependent dropdowns example):
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'quotes-form',
'enableAjaxValidation'=>false,
)); ?>
...
<div class="row">
<?php echo $form->labelEx($model,'package_id'); ?>
<?php $package_criteria = new CDbCriteria; $package_criteria->order = 'name ASC'; ?>
<?php echo $form->dropDownList($model, 'package_id', CHtml::listData(Package::model()->findAll($package_criteria),'id','name'),
// the original example's code was using the static array definition below, plus I had to call form builder for the dropdown list
// to have package_id parameter included and saved when submitting the form; same applies to voyage id dropdown list below
//<?php echo CHtml::dropDownList('package_id','', array(1=>'Package 1',2=>'Package 2',3=>'Package 3'),
array(
'empty'=>'Select a package', // set default prompt
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Package/listvoyages'), //url to call.
// I had to modify to-be-updated field name, including model form's prefix, to make AJAX update work
'update'=>'#Quotes_voyage_id', //selector to update
//'data'=>'js:javascript statement'
// I had to explicitely pass my parameter as 'package_id' instead of adding model form's prefix in controller action (see below)
// making Package controller's 'listvoyages' action reusable
'data'=>array('package_id'=>'js:this.value'),
)));
?>
<?php // added to let user create a package on the fly using CJuiDialog, as per Zaccaria's wiki article
echo CHtml::link('Create package', "", // the link for open the dialog
array(
'style'=>'cursor: pointer; text-decoration: underline;',
'onclick'=>"{addPackage(); $('#dialogPackage').dialog('open');}"));?>
<?php echo $form->error($model,'package_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'voyage_id'); ?>
<?php echo $form->dropDownList($model, 'voyage_id', array(), array()); ?>
<?php echo $form->error($model,'voyage_id'); ?>
</div>
...rest of the form
<?php $this->endWidget(); ?>
The above view makes an AJAX call to my Package controller’s ‘listvoyages’ action, defined like this:
public function actionListvoyages()
{
$data=Voyage::model()->findAll('package_id=:package_id', array(':package_id'=>(int) $_POST['package_id']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
}
}
Well, the above code works fine in Chrome 13.0.782.112 but it doesn’t in Firefox 6.0, nor in IE 7.0 and Safari 4.0.4 (voyage dropdown doesn’t get updated).
Particularly in FF, Firebug shows that the AJAX call receives parameter ‘package_id’ correctly but it returns my app’s login page.
Any idea?
Hoping comments may help someone, TIA.
rash*