Help with undefined index

Hi,

I am having a problem running an action in my controller and the error is:

undefined index reservation. Can anyone please help me find the cause of the error?

Here is my code:

View





<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(


	'id'=>'reservation-form',


	'enableAjaxValidation'=>false,


)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php echo $form->errorSummary($model); ?>




	<div class="row">


		<?php echo $form->labelEx($model,'date'); ?>

		<?php echo $form->textField($model,'date',array('disabled'=>'disabled')); ?>

		<?php echo $form->error($model,'date'); ?>

	</div>





	<div class="row">


		<?php echo $form->labelEx($model,'agency'); ?>

		<?php echo $form->dropDownList($model,'agency',

 		CHtml::listData(Agency::model()->findAll(), 'agency_id', 'agency_name'),array('prompt' => 'Select')); ?>

		<?php echo $form->error($model,'agency'); ?>

	</div>


	<div class="row">


		<?php echo $form->labelEx($model,'group'); ?>

		<?php echo $form->textField($model,'group',array('size'=>25,'maxlength'=>25)); ?>

		<?php echo $form->error($model,'group'); ?>

	</div>





	<div class="row">


		<?php echo $form->labelEx($model,'client'); ?>

		<?php echo $form->dropDownList($model,'client',

 		CHtml::listData(Client::model()->findAll(array('order'=>'name ASC')), 'id_client', 'FullName'),array('prompt'=>'Select')); ?>	 			

		<?php echo $form->error($model,'client'); ?>

	</div>


	<div class="row">


		<?php echo $form->labelEx($model,'roomtype'); ?>

		<?php echo $form->dropDownList($model,'roomtype',

 		CHtml::listData(Roomtype::model()->findAll(), 'id', 'DescripId'),array('id'=>'rmtype')); ?>				

		<?php echo $form->error($model,'roomtype'); ?>

	</div>

	<div class="row">


		<?php echo $form->labelEx($model,'adults'); ?>

		<?php echo $form->textField($model,'adults',array('id'=>'ad','size'=>2)); ?>

		<?php echo $form->error($model,'adults'); ?>

	</div>


	<div class="row">


		<?php echo $form->labelEx($model,'children'); ?>

		<?php echo $form->textField($model,'children',array('id'=>'ch','size'=>2)); ?>

		<?php echo $form->error($model,'children'); ?>

	</div>


	<div class="row">


		<?php echo $form->labelEx($model,'arrival'); ?>

		<?php  $this->widget('zii.widgets.jui.CJuiDatePicker', array(

		'name' => CHtml::activeName($model, 'arrival'),

		'model'=>$model,

		'id'=>'arr',

		'value' => $model->arrival,

		'options'=>array(

		'showAnim'=>'fold',

		'dateFormat'=>'dd-mm-yy'

		),

		));    ?>		

		<?php echo $form->error($model,'arrival'); ?>

	</div>





	<div class="row">


		<?php echo $form->labelEx($model,'departure'); ?>

		<?php  $this->widget('zii.widgets.jui.CJuiDatePicker', array(

		'name' => CHtml::activeName($model, 'departure'),

		'model'=>$model,

		'id'=>'dep',		

		'value' => $model->departure,

		'options'=>array(

		'showAnim'=>'fold',

		'dateFormat'=>'dd-mm-yy'

		),

		));    ?>				

		<?php echo $form->error($model,'departure'); ?>

	</div>

        <div class="row">


		<?php echo $form->labelEx($model,'no_days'); ?>

		<?php echo $form->textField($model,'no_days',array('readonly'=>'readonly','size'=>2,'id'=>'nbjour')); ?>

		<?php echo $form->error($model,'no_days'); ?>

	</div>





	<div class="row">


		<?php echo $form->labelEx($model,'amount'); ?>

		<?php echo $form->textField($model,'amount',array('id'=>'amt','size'=>5)); ?>

		<?php echo $form->error($model,'amount'); ?>

	</div>





	<div class="row">


		<?php echo $form->labelEx($model,'left'); ?>

		<?php echo $form->textField($model,'left',array('id'=>'bal')); ?>

		<?php echo $form->error($model,'left'); ?>

	</div>




	<div class="row">


		<?php echo $form->labelEx($model,'room'); ?>

		<?php echo $form->dropDownList($model,'room',$room);?>

      <?php //echo $form->DropDownList($model,'room',array()); ?>            						

		<?php //echo $form->textField($model,'room'); ?>

   	<?php echo CHtml::ajaxButton('Update rooms',$this->createUrl('reservation/roomupdate'),

    	array('ajax'=>array('type'=>'POST','update'=>'#'.CHtml::activeId($model,'room'))));?>

	

    	

 		<?php echo $form->error($model,'room'); ?>

	</div>

	<div class="row buttons">


		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>





<?php $this->endWidget(); ?>




</div><!-- form -->

[/PHP]


Here is the action called by url /reservation/roomupdate in the controller

[php]


<?php




class ReservationController extends Controller

{

	


	public function actionRoomupdate()


	{

  		$roomtype = $_POST['Reservation']['roomtype'];

   	        $arrival = $_POST['Reservation']['arrival'];

  		$departure = $_POST['Reservation']['departure'];


		list($d, $m, $y) = explode('-', $arrival);

		$mk=mktime(0, 0, 0, $m, $d, $y);

		$arrival = date ('Y-m-d', $mk);

	

		list($d, $m, $y) = explode('-', $departure);

		$mk=mktime(0, 0, 0, $m, $d, $y);

		$departure = date ('Y-m-d', $mk);


                $data=Room::model()->findAll('roomtypeid=:id',

                array(':id'=> $roomtype));

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

                foreach($data as $value=>$id)  {                

                echo CHtml::tag('option',                   

                array('value'=>$value),CHtml::encode($id),true);            

                }        

	}


	public function actionCreate()


	{


		$model=new Reservation;

		$model->date= date('d-m-Y');

		// Uncomment the following line if AJAX validation is needed


		// $this->performAjaxValidation($model);





		if(isset($_POST['Reservation']))


		{


			$model->attributes=$_POST['Reservation'];

              if (!isset($_POST['noValidate']))

            {

                $valid=$model->validate();

                             if($valid)

                {

                    $model->save();

                    $this->redirect(array('view','id'=>$model->id));

                }

            }




		}





		$this->render('create',array(


			'model'=>$model,'depositManager'=>$depositManager


		));


	}





	/**


	 * Updates a particular model.


	 * If update is successful, the browser will be redirected to the 'view' page.


	 * @param integer $id the ID of the model to be updated


	 */


	public function actionUpdate($id)


	{


		$model=$this->loadModel($id);

	

		// Uncomment the following line if AJAX validation is needed


		// $this->performAjaxValidation($model);





		if(isset($_POST['Reservation']))


		{


			$model->attributes=$_POST['Reservation'];


			if($model->save())

			$this->redirect(array('view','id'=>$depot->reservation_id));

			


		}





		$this->render('update',array(


			'model'=>$model,


		));


	}			




In you actionRoomupdate method in controller,

as you update the data, where loadModel?

even there is nothing found in your controller which will update the data.

and confusion is,

[b]which page you are rendering?

where is page render code?[/b]

Where do you set your $room variable? I do not see it anywhere…


<?php echo $form->dropDownList($model,'room',$room);?>

Oh, I forgot to uncomment the line. Here it is:





	<div class="row">


      <?php echo $form->labelEx($model,'room'); ?>

      <?php echo $form->DropDownList($model,'room',array()); ?>            						

      <?php echo CHtml::ajaxButton('Update rooms',$this->createUrl('reservation/roomupdate'),

    	array('ajax'=>array('type'=>'GET','update'=>'#'.CHtml::activeId($model,'room'))));?>

	

Am I missing something?

For the controller, I have isolated only the required code but it contains all the necessary actions like loadmodel, etc.

The controller contains all the required actions you mentioned. I have given only the required code where there is a problem.

The first line of the method actionRoomupdate is not being carried out due to undefined index.

Thanks

There was also some errors in the controller in the method actionRoomupdate where it refers to the <id> of the three values. I have corrected it thus:







	public function actionRoomupdate()


	{

  	$roomtype = $_POST['Reservation']['rmtype'];

   	$arrival = $_POST['Reservation']['arr'];

  	$departure = $_POST['Reservation']['dep'];




The S_POST[‘Reservation’] is not found.

Thanks for help

As you haven’t post whole controller code, I can’t guess what you pass on render page.

had you pass Reservation model on render page?

Are you sure the $_POST[‘Reservation’]… is correct?


public function actionRoomupdate()


{

    $roomtype = $_POST['Reservation']['roomtype'];

    $arrival = $_POST['Reservation']['arrival'];

    $departure = $_POST['Reservation']['departure'];

I see in the view that the form’s id is actually ‘reservation-form’ so perhaps the form is named the same rather than ‘Reservation’?

Doh, beaten to it :)

I tried this also in despair :( but in vain. The method actionUpdate and actionCreate refer also to S_POST[‘Reservation’], so I guess it should be correct. Shouldn’t it?

The action is supposed to update the dropdown list in the form. It is found in the following line:




	<div class="row">


      <?php echo $form->labelEx($model,'room'); ?>

      <?php echo $form->dropDownList($model,'room',array()); ?>            	

      <?php echo CHtml::ajaxButton('Update rooms',$this->createUrl('roomupdate'),

    	array('ajax'=>array('type'=>'POST','update'=>'#'.CHtml::activeId($model,'room'))));?>

	

    	

 		<?php echo $form->error($model,'room'); ?>

	</div>



The ajax call is not sending any data, that’s why you don’t have $_POST[‘Reservation’] on the server side…

Thank you. Can you please tell me what I am missing? I am struggling with this for hours! :-[

No it we can give form id any name,

if not specified it will call its related controller method i.e actionRoomupdate.

Agree with mdomba.

You are missing the data ofcourse :D

check the ajax documentation - http://api.jquery.com/jQuery.ajax/

you need to send the data you need by using the "data" attribute of the ajax option

I suppose I am not using the ajaxButton properly? Problem with the syntax?

As I wrote above you need to send the data by using the data option…

something like this


<?php echo CHtml::ajaxButton('Update rooms',$this->createUrl('roomupdate'),

        array('ajax'=>array(

       	'type'=>'POST',

       	'data'=>'js:$("#reservation-form").serialize()',

       	'update'=>'#'.CHtml::activeId($model,'room')

        )

));?>



Thank you very much. I amended the code like you said and had also to modify some more things as follows:-

In the view file




	<div class="row">


	<?php echo $form->labelEx($model,'room'); ?>

	<?php echo $form->dropDownList($model,'room',array()); ?>            						

	<?php //echo $form->textField($model,'room'); ?>

   	<?php echo CHtml::ajaxButton('Update rooms',$this->createUrl('roomupdate'),

	array('type'=>'POST','data'=>'js:$("#reservation-form").serialize()','update'=>'#'.CHtml::activeId($model,'room')));?>

	

    	

 		<?php echo $form->error($model,'room'); ?>

</div>



In the controller file, thus:




 	$roomtype = $_POST['Reservation']['roomtype'];

   	$arrival = $_POST['Reservation']['arrival'];

  	$departure = $_POST['Reservation']['departure'];



I was using the ids that I gave to these 3 attributes in the form and it does not work that way. I had to use the real attribute names.

Now I must add some more conditions relative to the dates of departure and arrival on the findAll() in the method Roomupdate of the controller. Hope I succeed :D

Thanks again