脚本宝典收集整理的这篇文章主要介绍了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)时,它会尝试再次发布表单.
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)); }
以上是脚本宝典为你收集整理的php – 在YII Framework中提交按钮后保持当前页面全部内容,希望文章能够帮你解决php – 在YII Framework中提交按钮后保持当前页面所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。