Hi everyone! I have a problem with working retrieve post variable.
Prepare:
- models directory: models/Muser class created and method checkLogin($username, $psw)
body of method
public function checkLogin($username, $psw)
{
if($username == 'a' && $psw == 'b') return 1;
return -1;
}
- controllers directory: controller/UserController class with actionLogin()
Now in body of method
public function actionLogin()
{
$model = new Muser(); // initial Muser object
$username = Yii::$app->request->post('username');
$password = Yii::$app->request->post('password');
if($model->checkLogin($username, $password) == 1) {
echo "Yes";
} else {
return $this->render('loginForm');
}
}
- views directory: views/user/loginForm.php
<div id="formLogin">
<?php $form = ActiveForm::begin([
'action' => null,
'method' => 'post',
]); ?>
<?= $form->field($model, "username")->textInput() ?>
<?= $form->field($model, "password")->passwordInput() ?>
<div class="form-group">
<?= Html::submitButton('Check value!', ['class' => 'btn btn-primary']) ?>
</div>
<?php $form = ActiveForm::end(); ?>
</div>
When i run web/index.php?r=user/login and input username field = ‘a’ AND input password field = ‘b’ but nothing to display! Looking for your help! Thanks so much!
Sorry i’ve post wrong discussion for Yii 2.0