JUI Dialog

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 :P.

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…

Hi monoman81,

Try echoing all the variables being posted by


var_dump($_POST)

in your controller to see if "idViaje" is included, and see what variable you should use.

Hi macinville…

var_dump gives me nothing:

array[0]{}

Weird, if i use $(’#viajes-form’).serialize() (‘viajes-form’ is the id of the form) instead of

$(this).serialize() i can succesfully retrieve the ID that i want. But, something strange happens, in the form that appears in the JUI Dialog, after i capture all the data and try to send it to the server to save the new SERVICE record, $_POST[‘Serviciosviaje’] has nothing, so, the form is again showed empty and no data is saved…

in my case the form tag does not appear in the dialog, simply disappears.

can someone help?