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 ??
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));
}
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With