Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stay on current page after submit button in YII Framework

Tags:

php

yii

I am trying to submit a form , after form submition the current page should stay on with user entered datas . how to achieve this ?

public function actionUpload()
    {

       $model=new UploadModel();
       $basemodel=new BaseContactList();
       $importmodel=new ImportedFilesModel(); 


        $importmodel->name =$basemodel->name;  
        $importmodel->import_date = $now->format('Y-m-d H:i:s');
        $importmodel->server_path = $temp;
        $importmodel->file_name = $name;
        $importmodel->crm_base_contact_id = $crm_base_contact_id;

        if ($importmodel->save())
            echo "Import saved";
        else 
            echo "Import Not Saved";
        unset($_POST['BaseContactList']);    
        $this->redirect(Yii::app()->request->urlReferrer);

    }

This line "$this->redirect(Yii::app()->request->urlReferrer);" goes to previous page but the user entered values are cleared . How to redirect to previous page without clearing the values in the form ??

like image 756
US-1234 Avatar asked Nov 01 '22 08:11

US-1234


2 Answers

Same with when you view the error message after failed model saving. Instead of doing redirection, you can just pass the saved model to the form.

public function actionIndex(){
    $model = new Model();
    if (isset($_POST[get_class($model)]){
       $model->setAttributes($_POST[get_class($model)]);
       if ($model->save()){
          //do nothing
          //usually people do a redirection here `$this->redirect('index');`
          //or you can save a flash message
          Yii::app()->user->setFlash('message', 'Successfully save form');
       } else {
          Yii::app()->user->setFlash('message', 'Failed to save form');
       }
    }
    //this will pass the model posted by the form to the view,
    //regardless whether the save is successful or not.
    $this->render('index', array('model' => $model));
}

In the index view you can do something like.

<?php if (Yii::app()->user->hasFlash('message')):?>
    <div class="message"><?php echo Yii::app()->user->getFlash('message');?></div>
<?php endif;?>
<?php echo CHtml::beginForm();?>
    <!-- show the form with $model here --->
<?php echo CHtml::endForm();?>

The downside is, when you accidentally hit Refresh button (F5), it will try to post the form again.

Or you can save it using user session using setFlash.

public function actionUpload()
{

   $model=new UploadModel();
   $basemodel=new BaseContactList();
   $importmodel=new ImportedFilesModel(); 


    $importmodel->name =$basemodel->name;  
    $importmodel->import_date = $now->format('Y-m-d H:i:s');
    $importmodel->server_path = $temp;
    $importmodel->file_name = $name;
    $importmodel->crm_base_contact_id = $crm_base_contact_id;

    if ($importmodel->save())
        echo "Import saved";
    else 
        echo "Import Not Saved";
    unset($_POST['BaseContactList']);    

    //here we go
    Yii::app()->user->setFlash('form', serialize($basemodel)); 
    //
    $this->redirect(Yii::app()->request->urlReferrer);

}

In the previous form, you load the value from the session.

public function actionForm(){
    if (Yii::app()->user->hasFlash('form')){
       $basemodel = unserialize(Yii::app()->user->getFlash('form');
    } else {
       $basemodel = new BaseContactList();
    }
    $this->render('form', array('basemodel' => $basemodel));
}
like image 127
Petra Barus Avatar answered Nov 09 '22 15:11

Petra Barus


I do not know whether really such a long code is needed to do such a thing but I can simply do it by this.

Suppose i have an actionUpdate

public function actionUpdate($id)
    {   


        $model=$this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

                if(isset($_POST['Products']))
        {
            $model->attributes=$_POST['Products'];
                        $model->save();
                   // remove this redirect() line
        // $this->redirect(array('view', 'id' => $model->productId));       
            }

        $this->render('update',array(
            'model'=>$model,
        ));
    }

If in your action, you are redirecting after saving your data then just remove that line and you are done, provided that you are rendering the form again in the following lines
If you are not rendering the form in the next lines then you can do it yourself like

if($model->save())
{
$this->render('update',array(
                'model'=>$model,
            ));
Yii::app()->end();
} 
like image 40
Let me see Avatar answered Nov 09 '22 16:11

Let me see