E
E
Eugene2018-06-06 11:44:01
Yii
Eugene, 2018-06-06 11:44:01

Why can't he see Flash? Where did I go wrong?

In general, I send a message from the form to the database.
Here is the form

The form
<?php if (Yii::$app->session->hasFlash('contactFormSub')): ?>

                    <div class="alert alert-success">
                        Ваше сообщение отправлено, в ближайшее время наши специалисты дадут Вам ответ на указнную вами электронную почту.
                    </div>
                <?php else: ?>
                <div class="contact-form">
                    <!-- contact form start -->
                    <?php $form = ActiveForm::begin(['id' => 'messages','options' => ['class' => 'row'], 'fieldConfig' => ['options' => ['tag' => false]]]); ?>
                        <!-- name -->
                        <div class="col-md-6">
                            <?= $form->field($model, 'nameU')->textInput(['autofocus' => true, 'class' =>'form-control main', 'placeholder' => 'Имя', 'required' => true])->label(false)?>
                        </div>
                        <!-- email -->
                        <div class="col-md-6">
                            <?= $form->field($model, 'emailU')->textInput(['autofocus' => true, 'class' =>'form-control main', 'placeholder' => 'Почта', 'required' => true])->label(false)?>

                        </div>
                        <!-- phone -->
                        <div class="col-md-12">
                            <?= $form->field($model, 'telephoneU')->textInput(['autofocus' => true, 'class' =>'form-control main', 'placeholder' => 'Телефон', 'required' => true])->label(false)?>
                        </div>
                        <!-- message -->
                        <div class="col-md-12">
                            <?= $form->field($model, 'textU')->textarea(['autofocus' => true, 'class' =>'form-control main', 'placeholder' => 'Текст сообщения', 'required' => true, 'rows' =>15])->label(false)?>

                        </div>
                        <!-- submit button -->
                        <div class="col-md-12 text-center">
                            <?= Html::submitButton('Отправить', ['class' => 'btn btn-style-one', 'name' => 'contact-button']) ?>
                        </div>
                    </form>
                    <!-- contact form end -->
                    <?php ActiveForm::end(); ?>
                </div>

                <?php endif; ?>

Well, I wanted to make it so that when sending the form it disappeared and a message appeared that it was sent.
Here in the controller
public function actionContacts()
    {
        $model = new Messages() ;
        if ($model->load(Yii::$app->request->post()) && $model->message()) {
            Yii::$app->session->setFlash('contactFormSub');
            return $this->refresh();
        }

        return $this->render('contacts', ['model' => $model]);
    }

And the method itself in the model
public function Message()
    {
        $model = new Messages();
        $model->name = $this->nameU;
        $model->email = $this->emailU;
        $model->telephone = $this->telephoneU;
        $model->text = $this->textU;
        $model->save();

    }

But when sending a message, the page is simply updated (and the values ​​​​in the fields remain), the message in the database reaches everything ok. By the way, with further page updates, a sign pops up and always submits.
But the problem is that the form was not replaced by a message about the successful submission. Where did I screw up?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-06-06
@webinar

You are using flash messages in a very strange way, I recommend:
https://www.yiiframework.com/doc/guide/2.0/en/runt...
But it should still work. And session normally works? Are you sure that

if ($model->load(Yii::$app->request->post()) && $model->message()) {
does it work? Try addFlash instead of setFlash

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question