Thanks peeps. But the reason it wasn’t POSTing was due to the fact there was no form tag being created. That wiki article should be updated.
I only have slight problem, if they is only one value in the dropdown, then you cannot change it, therefore the field below is cannot be populated and the form is incomplete. The workaround I used was echo statements like (echo ‘<option value="">Choose a date</option>’).
Also, if all the fields have the correct values, then the user decides to change the film, then the lowest field, still has the time from the last film.
What do you guys think? Any better solutions?
Here’s the code:
View: _form.php
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'booking-form',
'enableAjaxValidation'=>false,
));
$film=Film::model()->findAll();
$filmlist=CHtml::listData($film,'film_id','title'); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo CHtml::dropDownList('film_id','', $filmlist, array('empty' => 'Choose a film', 'ajax' => array('type'=>'POST','url'=>CController::createUrl('getDatesShowing'),'update'=>'#start_date'))); ?>
</div>
<div class="row">
<?php echo CHtml::dropDownList('start_date','', array(), array('ajax' => array('type'=>'POST','url'=>CController::createUrl('getTimesShowing'),'update'=>'#showing_id'))); ?>
</div>
<div class="row buttons">
<?php echo CHtml::dropDownList('showing_id','', array()); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Create'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Controller: BookingController.php
...
public function actionCreate()
{
$model=new Booking;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['showing_id']))
{
$model->showing_id=$_POST['showing_id'];
$model->user_id=Yii::app()->user->getId();
if($model->save())
$this->redirect(array('view','id'=>$model->booking_id));
}
$this->render('create',array(
'model'=>$model,
));
}
public function actionGetDatesShowing()
{
$data=Showing::model()->findAll('film_id=:film_id', array(':film_id'=>(int) $_POST['film_id']));
$data=CHtml::listData($data,'start_date','start_date');
echo '<option value="">Choose a date</option>';
foreach($data as $value=>$name)
{
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
}
}
public function actionGetTimesShowing()
{
$data=Showing::model()->findAll('start_date=:start_date', array(':start_date'=>$_POST['start_date']));
$data=CHtml::listData($data,'showing_id','start_time');
echo '<option value="">Choose a time</option>';
foreach($data as $value=>$name)
{
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
}
}
...