mercredi 3 mai 2017

Saving Form Data Into Database Table YII2

So I'm trying to get information through a Yii2 form and save it into the database, though it won't work. I'm getting the success flash message, but no changes are made into the database.

Controller file:

<?php

namespace frontend\modules\portfolio\controllers;

use Yii;
use yii\web\Controller;
use frontend\modules\portfolio\models\LandingPage;
use common\models\HelloMessage;

class HelloController extends Controller {

    public function actionIndex() {

        $form_model = new HelloMessage();
        $request    = Yii::$app->request; 

        if ($form_model->load(Yii::$app->request->post())) {
            $form_model->name = $request->post('name');
            $form_model->email = $request->post('email');
            $form_model->message = $request->post('message');

            $form_model->save();
            Yii::$app->getSession()->setFlash('success', 'Your message has been successfully recorded.');
        }

        return $this->render('index', [
            'form_model' => $form_model
        ]);
    }

}

And this would be the View file:

        <div class="box">
            <?= Yii::$app->session->getFlash('success'); ?>

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

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

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

            <?= $form->field($form_model, 'message')->textarea(['maxlength' => true]) ?>

            <div class="form-group">
                <?= Html::submitButton('Submit', ['name' => 'contact-button']); ?>
            </div>

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




Aucun commentaire:

Enregistrer un commentaire