Dependent dropdown Not working

I’ve been trying, I still can’t get it to work.

This is the code (ignore the weird names):

index.php?r=die




$film=Film::model()->findAll();

$filmlist=CHtml::listData($film,'film_id','title');


echo CHtml::dropDownList('film_id','', $filmlist,

	array(

	'ajax' => array(

	'type'=>'POST',

	'url'=>CController::createUrl('kill'),

	'update'=>'#showing_id',

))); 

//empty since it will be filled by the other dropdown

echo CHtml::dropDownList('showing_id','', array());



index.php?r=die/kill




public function actionIndex()

{

	$this->render('index',array());		

}


public function actionKill()

{

	$data=Showing::model()->findAll('film_id=:film_id', array(':film_id'=>(int) $_POST['film_id']));

	$data=CHtml::listData($data,'showing_id','showing_id');

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

	{

		echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);

	}

}



Replacing “(int) $_POST[‘film_id’]” with a number give me values correctly. Using Firebug, I cannot see post request, is blank.

This is the response.

Can someone please tell me what I’m doing wrong. Just need the id (int) to be sent to the next form.

Have a look at

http://www.yiiframework.com/wiki/24/

http://www.yiiframework.com/forum/index.php?/topic/25358-creating-a-dependent-dropdown/page__view__findpost__p__122306__fromsearch__1

http://www.yiiframework.com/forum/index.php?/topic/25359-solved-ajax-to-hide-form-div-based-on-dropdown/page__view__findpost__p__122318__fromsearch__1

http://www.yiiframework.com/forum/index.php?/topic/11531-solved-how-to-create-dependent-dropdown-lists/

Do you know jQuery POST function. If you interested then i can send my custom formula for dependent selection box for YiiFramework.

It’s based on the one here: http://www.yiiframework.com/wiki/24/

Does anyone see any problems?

Rajib could I have a look at yours?

If I not a normal POST request it works, but display the data on die/kill.

It was due to the missing form element. The person that posted that article should really check to see if the code works before it is posted on the wiki.

Click Here

I think it will help you. If any problem please tell me.

Thank You

how if you change code this line :




echo CHtml::dropDownList('film_id','', $filmlist,

        array(

        'ajax' => array(

        'type'=>'POST',

        'url'=>CController::createUrl('kill'),

        'update'=>'#showing_id',

))); 



to be




echo CHtml::dropDownList('film_id','film_id', $filmlist,

        array(

        'ajax' => array(

        'type'=>'POST',

        'url'=>CController::createUrl('kill'),

        'update'=>'#showing_id',

))); 



i seem that you put model data but not put attribute for film_id

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

	}

}

...



Found a huge issue with this, when the times are generated, it’s for all films on the selected date, not just for the selected film.

This is because I’m not POSTing the film_id on the second dropdown, but Yii only allows one value to be posted.

HELP?