Wednesday 15 February 2012

yii framework popup example


in this example i will implement a simple popup form to create action from controller.i am using the gii for generating the code for the CRUD

code inside controller
 public function actionCreate()  
     {  
         $model=new ExampleModel;  
         // Uncomment the following line if AJAX validation is needed  
         //$this->performAjaxValidation($model);  
          if(isset($_POST['Apply']))  
          {  
             $model->attributes=$_POST['Apply'];  
          if($model->save()){ //save the record to the database  
        if (Yii::app()->request->isAjaxRequest) // check for ajax request  
          {  
         echo CJSON::encode(array(  // display message  
                   'status'=>'success',  
                   'div'=>"Classroom successfully added"  
               ));  
              exit;         
      }  
      else{  
    $this->redirect(array('post/view','id'=>$model->aid)); // if the condition fail redirect the user to post/view  
                       }  
                   }  
         }  
   if (Yii::app()->request->isAjaxRequest) // check the condition  
   {  
    echo CJSON::encode(array(  
    'status'=>'failure',  
    'div'=>$this->renderPartial('_form', array('model'=>$model), true)));  
      exit;         
   }  
   else{  
      $this->render('create',array(  // return the form  
      'model'=>$model,  
    ));  
                 }  
  $this->refreash();  
     }  
 below section is the code need to insert inside your view file where ever you want the popup to be displayed  
 code inside your view  
 <?php  
  echo CHtml::link('apply for job', "", // the link for open the dialog  
   array(  
     'style'=>'cursor: pointer; text-decoration: underline;',  
     'onclick'=>"{addClassroom(); $('#dialogClassroom').dialog('open');}"));  
 ?>  
 <?php  
 $this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog  
   'id'=>'dialogClassroom',  
   'options'=>array(  
     'title'=>'Create classroom',  
     'autoOpen'=>false,  
     'modal'=>true,  
     'width'=>550,  
     'height'=>470,  
   ),  
 ));?>  
 <?php  
 $this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog  
   'id'=>'dialogClassroom',  
   'options'=>array(  
     'title'=>'Create classroom',  
     'autoOpen'=>false,  
     'modal'=>true,  
     'width'=>550,  
     'height'=>470,  
   ),  
 ));?>  
 <div class="divForForm"></div>  
 <?php $this->endWidget();?>  
 <script type="text/javascript">  
 // here is the magic  
 function addClassroom()  
 {  
   <?php echo CHtml::ajax(array(   // code for the javascript  
       'url'=>array('apply/create&id='.$model->id),  
       'data'=> "js:$(this).serialize()",  
       'type'=>'post',  
       'dataType'=>'json',  
       'success'=>"function(data)  
       {  
         if (data.status == 'failure')  
         {  
           $('#dialogClassroom div.divForForm').html(data.div);  
              // Here is the trick: on submit-> once again this function!  
           $('#dialogClassroom div.divForForm form').submit(addClassroom);  
         }  
         else  
         {  
           $('#dialogClassroom div.divForForm').html(data.div);  
           setTimeout(\"$('#dialogClassroom').dialog('close') \",300);  
         }  
       } ",  
       ))?>;  
   return false;  
 }  
 </script>