微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 在YII Framework中提交按钮后保持当前页面

我正在尝试提交表单,在表单子目录后,当前页面应保持用户输入的数据.怎么实现这个?
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-> redirect(Yii :: app() – > request-> urlReferrer);”转到上一页用户输入的值已清除.如何在不清除表单中的值的情况下重定向上一页

保存模型失败后查看错误消息时相同.您可以将保存的模型传递给表单,而不是进行重定向.
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));
}

在索引视图中,您可以执行类似的操作.

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

缺点是,当您不小心点击“刷新”按钮(F5)时,它会尝试再次发布表单.

或者您可以使用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);

}

在上一个表单中,您将加载会话中的值.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐