Hi,
Last week I was able to update a div through ajax, using the renderPartial() method. I was very happy about how easy it was using Yii.
Now I am running into problem when I want to do the same thing, on another page in the same application.
In my view I have this code, it’s a dropdownlist that triggers an ajax request when selecting a value:
<?php
$available_rooms = Room::model()->getAvailableRoomNumbersForSelect($reservation->checkin, $reservation->checkout);
echo CHtml::beginForm();
echo CHtml::dropDownList('add_room', '0', $available_rooms, array(
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('reservation/addRoom'),
'update'=>'#booking_detail', //selector to update
'data'=>array('room_id'=>'js:this.value', 'reservation_id'=>$reservation->reservation_id),
),
'confirm'=>'Are you sure you want to add this room?',
'options' => array(0 => array('disabled'=>true)),
));
echo CHtml::endForm();
?>
The same view also has this code to generate the partial view:
<div id="booking_detail">
<?php echo $this->renderPartial('_ajaxBooking', array('reservation'=>$reservation)); ?>
</div>
This is the controller action:
public function actionAddRoom() {
$room_id = $_POST['room_id'];
$reservation_id = $_POST['reservation_id'];
// double check if room is still available
// add room to booking by adding record to room_has_reservation
$room_has_reservation = new RoomHasReservation();
$room_has_reservation->setAttribute('room_id', $room_id);
$room_has_reservation->setAttribute('reservation_id', $reservation_id);
if($room_has_reservation->save()) {
}
// use renderpartial to update booking detail view
$reservation = Reservation::model()->findByAttributes(array('reservation_id'=>$reservation_id));
$this->renderPartial('_ajaxBooking', $reservation, false, true);
}
The partial view being rendered contains only this code:
<?php
echo $this->renderPartial('_booking_detailview', array('reservation'=>$reservation));
Now, the page is working fine, and a room gets added with the ajax request. But the div is not being updated.
When I inspect the output in Firebug, I see this error in the response:
<h1>PHP Error [8]</h1>
<p>Undefined variable: reservation (C:\wamp\www\yiihotelbooking\protected\views\reservation\_ajaxBooking.php:5)</p>
So, why the hell is the variable not available in the partial view ?
I am passing the variable in exactly the same way as I did on the other page, where it is working.
Am I overlooking something?
Thx Edwin