Hi there,
In a view form, I need to fill up a dropdown list which is dependent on three values of my form. Can anyone suggest how I can do this? These 3 values are already being used in a jquery $post after a change event to update other values in the form. I can have an array returned by a S.get Jquery, but how to put the values of this array in the php variable ($room) from where the dropdown list must take its values?
Here is my code:
View _form.php
<div class="form">
<?php Yii::app()->getClientScript()->registerScript('duration','$("#arr,#dep").change(function()
{
if (!$("#dep").val()=="" && !$("#arr").val()=="")
{
var dat1=$("#arr").val();
var dat2=$("#dep").val();
var dat1=dat1.split("-");
var dat2=dat2.split("-");
dat1= new Date(dat1[2], dat1[1]-1, dat1[0]);
dat2= new Date(dat2[2], dat2[1]-1, dat2[0]);
nb=(dat2-dat1)/(1000*24*60*60);
if (nb<=0)
{alert("Invalid Dates");}
else
{
$("#nbjour").val(nb);
}
var nombre=parseInt($("#ad").val())+parseInt($("#ch").val());
$.get("index.php?r=reservation/Calcul", {brd: $("#brd").val(),rmtype: $("#rmtype").val(), nbr:nombre}, function(data)
{
var amt = nb*data.prix;
adad = $("#add_ad").val();
adch = $("#add_ch").val();
amt = amt+(nb*adad*data.prixadd)+(nb*adch*data.prixadd/2);
$("#amt").val(amt);
},"json");
}
}
);');
?>
<?php Yii::app()->getClientScript()->registerScript('checknoguest','$("#ch").blur(function()
{
$.post("index.php?r=reservation/GuestCheck", {ad: $("#ad").val(), ch: $("#ch").val(),rmtype: $("#rmtype").val()}, function(data)
{.........(here some values of this form are updated)
.........
.........
},"json");
}
);');
?>
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'reservation-form',
'enableAjaxValidation'=>false,
)); ?>
<?php
<p class="note">Fields with <span class="required">*</span> are required.</p>
<input type="hidden" name="test" id="test">
<?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,'client'); ?>
<?php echo $form->dropDownList($model,'client',
CHtml::listData(Client::model()->findAll(), 'id_client', 'FullName')); ?>
<?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,'board'); ?>
<?php echo $form->dropDownList($model,'board',
array('BO'=>'Bed Only', 'BB'=>'Bed & Breakfast', 'HB'=>'Half Board', 'FB'=>'Full Board'), array('id'=>'brd'));?>
<?php echo $form->error($model,'board'); ?>
</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->error($model,'room'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Controller
<?php
class ReservationController extends Controller
{
public function actionGuestCheck()
{
$ad = $_POST['ad'];
$ch = $_POST['ch'];
$rmtype= $_POST['rmtype'];
$result =Roomtype::model()->find("id='$rmtype'");
$test = array('nor'=>$result->maxnormal,'add'=>$result->maxadd);
header('Content-Type: application/json');
echo json_encode($test);
}
public function actionCalcul()
{
$brd = $_GET['brd'];
$rmtype= $_GET['rmtype'];
$nbr=$_GET['nbr'];
$result =Roomtype::model()->find("id='$rmtype'");
$array = array("one","two","three","four");
$libnbr = $array[$nbr-1];
$att = $libnbr.strtolower($brd);
$prix = $result->$att;
$att = 'add'.strtolower($brd);
$prixadd= $result->$att;
$result = array('prix'=>$prix, 'prixadd'=>$prixadd);
header('Content-Type: application/json');
echo json_encode($result);
}
I have not written the function in the controller which will yield the array result for the dropdown list but I think this should not be a too difficult issue for me. My problem is how to get this array values in the variable $room of my form.