S
S
Sergey2015-08-09 19:59:47
Yii
Sergey, 2015-08-09 19:59:47

How to make a redirect in yii2?

There is a form

<?php $form = ActiveForm::begin(); ?>
<?= Html::activeTextInput($model, 'sum', [ 'class' => 'form-control','name' => 'sumer', 'value' => '']) ?>
<?= Html::submitButton($model->isNewRecord ? Yii::t('frontend', 'Transfer') : Yii::t('frontend', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

controller
public function actionTrans()
    {

        $model = new Transactions();        
        if ($model->load(Yii::$app->request->post()) ) {

            return $this->redirect(['create']);
            
        } else {
            return $this->render('trans', [
                'model' => $model,
            ]);
        }
    }

form accept file
<?php
 $su =  ($_POST['sumer']);
echo $su;
?>

You need to make sure that when you click on submitting the form, it goes through the controller and is accepted by the post file.
I get a post data break in the controller and the create file does not accept anything, tell me how to solve this problem.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nikita, 2015-08-10
@bitver

You misunderstand the essence of the POST request, the framework has nothing to do with it. If you want to transfer some data from one request to another (in the case of Yii, this is between two actions), then you will have to save it somewhere. As an option, I can suggest storing them in the session Yii::$app->session->set()// ->get()and actionCreate()passing this data to the view

return $this->render('create', [
  'data1' => Yii::$app->session->get('someData1'),
  .....
],);

Although in your code you are loading POST data into the model, it can be saved after all
if ($model->load(Yii::$app->request->post()) ) { // Здесь вы грузите
            // Делаете что хотите
            $model->save(); // Сохранили
            return $this->redirect(['create']);
        }

And in another query (which will process your actionCreate()) pull it out of the base as the last element.

L
Lumore, 2015-08-09
@Lumore

<?php $form = ActiveForm::begin([
'action' => 'ваш урл'
]); ?>

A
Akim Glushkov, 2015-08-10
@mikaakim

Just include the file, right?
require "<----path to file---->";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question