Yii Framework Forum: Yii2 Basic Clear input form after submit button - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii2 Basic Clear input form after submit button

#1 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 16-December 17

Posted 11 January 2018 - 02:29 PM

I created two modals one to create a post and another one to create gallery inside another view and it work perfect but after I click submit my form keeps the data entered even if I refresh the page it's still have the data entered, is it possible to clear form after submit

The Post form View code is
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $ly_addPost app\models\Posts */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="posts-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($ly_addPost, 'Post_title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($ly_addPost, 'Post_text')->textarea(['rows' => 6]) ?>

    <?= $form->field($ly_addPost, 'Post_file')->textInput(['maxlength' => true]) ?>

    <?= $form->field($ly_addPost, 'Permission_id')->dropdownList([$ly_addPost->Permission_id]);?>

    <div class="form-group">
        <?= Html::submitButton('Create' , ['class' => 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>


and the controller code is
public function actionView($id)
    {
		
		$ly_addPost = new Posts();
		$ly_addGallery = new Galleries();

		$ly_addPost->Channel_id = $id;
		$ly_addGallery->Channel_id = $id;
		
		$ly_addPost->Userid = Yii::$app->user->id;
		$ly_addGallery->Userid = Yii::$app->user->id;
		
		if ($ly_addPost->load(Yii::$app->request->post()) ) {	
			
			$ly_addPost->Post_id = Yii::$app->params['ly_randCttid'];
			$ly_addPost->Post_uid = Yii::$app->params['ly_randCttuid'];
			
			$ly_addPost->save();	
			
			 foreach($ly_addPost as $property => $value)  {  
				
				$value =null;
				//var_dump ($value);
			}   
			return $this->render('view', [
				'model' => $this->findModel($id),
				'ly_addPost' => $ly_addPost,
				'ly_addGallery' => $ly_addGallery,
			]);
			
			exit(); 
		} else {
						
			return $this->render('view', [
				'model' => $this->findModel($id),
				'ly_addPost' => $ly_addPost,
				'ly_addGallery' => $ly_addGallery,
			]);
		}	
	}

I used $value = null ;
and even so it's not working
0

#2 User is offline   umneeq 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 206
  • Joined: 10-October 11
  • Location:Russia

Posted 12 January 2018 - 12:17 AM

Try redirect after saving instead of render
0

#3 User is offline   Bynd 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 16-December 17

Posted 12 January 2018 - 05:18 AM

View Postumneeq, on 12 January 2018 - 12:17 AM, said:

Try redirect after saving instead of render


Thank you for your replay But I fixed by using
return $this->refresh();


Now my code is

if ($ly_addPost->load(Yii::$app->request->post()) ) {   
                        
                        $ly_addPost->Post_id = Yii::$app->params['ly_randCttid'];
                        $ly_addPost->Post_uid = Yii::$app->params['ly_randCttuid'];
                        
                        $ly_addPost->save();    
                        
                        return $this->refresh();
                }

I changed
foreach($ly_addPost as $property => $value)  {  
                                
                                $value =null;
                                //var_dump ($value);
                        }   
                        return $this->render('view', [
                                'model' => $this->findModel($id),
                                'ly_addPost' => $ly_addPost,
                                'ly_addGallery' => $ly_addGallery,
                        ]);
                        
                        exit();


to
return $this->refresh();

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users