-1

I'm building a questionnaire site.
On this site the user enters his email to receive the result of his questionnaire. So this site has no authentication.

How do I store the user's email to the end of the questionnaire?

It is my User model:

<?php

namespace common\models;

use Yii;
use \yii\db\ActiveRecord;
// use yii\web\IdentityInterface;

/**
 * This is the model class for table "user".
 *
 * @property int $id
 * @property string $email
 * @property string $name
 * @property string $family
 * @property string $major
 * @property string $univercity
 * @property int $education
 * @property int $gender
 * @property int $age
 * @property int $income
 * @property int $get_result
 * @property int $created_at
 */
class User extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            // TimestampBehavior::className(),
        ];
    }


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['email', 'created_at'], 'required'],
            [['education', 'gender', 'age', 'income', 'get_result', 'created_at'], 'integer'],
            [['email', 'name', 'family', 'major', 'univercity'], 'string', 'max' => 255],
            [['email'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'email' => 'Email',
            'name' => 'Name',
            'family' => 'Family',
            'major' => 'Major',
            'univercity' => 'Univercity',
            'education' => 'Education',
            'gender' => 'Gender',
            'age' => 'Age',
            'income' => 'Income',
            'get_result' => 'Get Result',
            'created_at' => 'Created At',
        ];
    }
}

Hamed
  • 158
  • 12

1 Answers1

1

There are many ways of achieving that, it mostly depends on your logic under the hood.

One of the easiest is to use session.

First store the email in session:

\Yii::$app->session->set('questionnaire-email', $usersEmail);

Then, when you want to use it:

$email = \Yii::$app->session->get('questionnaire-email');
Bizley
  • 15,937
  • 5
  • 43
  • 53
  • I tried this solution. but after `redirect` or `refresh` session is empty – Hamed Aug 28 '19 at 07:56
  • 1
    This looks like some session misconfiguration, maybe cookies are cleared or something. In general session should be kept unless explicitly set different. – Bizley Aug 28 '19 at 08:02
  • If it's a multi page questionnaire you should consider the possibility that user will put filling it on hold and do other things. When he returns to it the session might be already expired. It might be a better idea to add some identifier into hidden input on each page of questionnaire. – Michal Hynčica Aug 29 '19 at 10:44