Hi, i have an issue with JUI dialog. And first of all, my best regards and excuse my English. It ain’t my native language .
I have two models: VIAJES (spanish for TRIPS) and SERVICIOS (spanish for SERVICES). Every TRIP has zero, one or more SERVICES. I want to add the SERVICES for the TRIP from the _form view of the TRIP. To accomplish this, i’m using a link which opens a JUI Dialog, and through ajax call the create action of the SERVICES Controller which do the magic. Here is the code that i’m using in the _form view of the model TRIPS to do what i want.
<div id="listaServicios"></div>
<?php
if (!$model->isNewRecord)
{
echo CHtml::link('Agregar Servicios', "", array(
'style'=>'cursor:pointer; text-decoration:underline;',
'onclick'=>"{addServicio(); $('#dialogServicio').dialog('open');}" ));
}
?>
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'dialogServicio',
'options'=>array(
'title'=>'Crear Servicio',
'autoOpen'=>false,
'modal'=>true,
'width'=>400,
'height'=>300,
),
));
?>
<div class="divForForm"></div>
<?php $this->endWidget('zii.widgets.jui.CJuiDialog'); ?>
<script type="text/javascript">
function addServicio()
{
<?php
echo CHtml::ajax(array(
'url'=>array('serviciosviaje/create'),
'data'=>"js:$(this).serialize()",
'type'=>'post',
'dataType'=>'json',
'success'=>"js:function(data)
{
if (data.status == 'failure')
{
$('#dialogServicio div.divForForm').html(data.div);
$('#dialogServicio div.divForForm form').submit(addServicio);
}
else
{
$('#dialogServicio div.divForForm').html(data.div);
if (data.returnval != '')
$('#listaServicios').append(data.returnval);
setTimeOut(\"$('dialogServicio').dialog('close')\",3000);
}
}",
))
?>;
return false;
}
</script>
And i have a hidden field which store the ID of the current record
<?php echo $form->hiddenField($model,'idViaje'); ?>
In the controller i have this:
public function actionCreate()
{
$model=new Serviciosviaje;
if(isset($_POST['Serviciosviaje']))
{
$model->attributes=$_POST['Serviciosviaje'];
if($model->save())
{
if (Yii::app()->request->IsAjaxRequest)
{
echo CJSON::encode(array(
'status'=>'success',
'div'=>'Servicio creado',
'returnval'=>CHtml::tag('p',array("id"=>"Servicio".$model->idServicio),
CHtml::encode($model->descripcion),true),
));
exit;
}
else
$this->redirect(array('view','id'=>$model->idServicio));
}
}
if (Yii::app()->request->IsAjaxRequest)
{
echo CJSON::encode(array(
'status'=>'failure',
'div'=>$this->renderPartial('_form',array(
'model'=>$model,'viaje'=>[color="#FF0000"][HERE I SHOULD PUT THE VALUE OF THE ID BUT I DON'T KNOW HOW][/color]),true),
));
exit;
}
else
$this->render('create',array(
'model'=>$model,
));
}
How do i read the value of the hidden field in the action create of the Controller??? I tried $_POST[“idViaje”] but didn’t work…