US-1234
US-1234

Reputation: 1529

Stay on current page after submit button in YII Framework

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 ??

Upvotes: 5

Views: 7949

Answers (4)

Let me see
Let me see

Reputation: 5094

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();
} 

Upvotes: 1

Dency G B
Dency G B

Reputation: 8146

Will this do?

if ($importmodel->save())
  $_SERVER['PHP_SELF'];

Upvotes: 0

Petra Barus
Petra Barus

Reputation: 4013

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));
}

Upvotes: 2

Jenno Richi Benat
Jenno Richi Benat

Reputation: 671

Simple Redirect to the same action and send the model loaded as an arguement

Upvotes: -1

Related Questions