Home >Backend Development >PHP Tutorial >YII framework cannot receive post parameters?

YII framework cannot receive post parameters?

WBOY
WBOYOriginal
2016-07-06 13:51:532292browse

Let’s start with the code. The error must be in these two, but I don’t know how to solve it. Attached URL: http://182.61.37.188/?r=login/entry

  1. LoginController

<code><?php
/**
 * Created by PhpStorm.
 * User: xin
 * Date: 16/6/29
 * Time: 上午9:28
 */

namespace app\controllers;
use Yii;
use yii\base\Controller;
use app\models\Userdb;
use app\models\EntryForm;

class LoginController extends Controller{
    public function actionEntry(){

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

            print_r($model);
            print_r(Yii::$app->request->post('userName'));
            $info = new  Userdb();
            $info->userName = $model->username;
            $info->password = $model->password;
            $info->save();

            return $this->render('showinfo', ['model' => $model]);
        }else{
            return $this->render('entry',['model' => $model]);
        }

    }
}</code>

2.entry.php

<code><?php
/**
 * Created by PhpStorm.
 * User: xin
 * Date: 16/6/29
 * Time: 上午9:37
 */

use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal'],
    'fieldConfig' => [
        'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
        'labelOptions' => ['class' => 'col-lg-1 control-label'],
    ],
]); ?>

<?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>

<?= $form->field($model, 'password')->passwordInput() ?>

    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
        </div>
    </div>

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

Reply content:

Let’s start with the code. The error must be in these two, but I don’t know how to solve it. Attached URL: http://182.61.37.188/?r=login/entry

  1. LoginController

<code><?php
/**
 * Created by PhpStorm.
 * User: xin
 * Date: 16/6/29
 * Time: 上午9:28
 */

namespace app\controllers;
use Yii;
use yii\base\Controller;
use app\models\Userdb;
use app\models\EntryForm;

class LoginController extends Controller{
    public function actionEntry(){

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

            print_r($model);
            print_r(Yii::$app->request->post('userName'));
            $info = new  Userdb();
            $info->userName = $model->username;
            $info->password = $model->password;
            $info->save();

            return $this->render('showinfo', ['model' => $model]);
        }else{
            return $this->render('entry',['model' => $model]);
        }

    }
}</code>

2.entry.php

<code><?php
/**
 * Created by PhpStorm.
 * User: xin
 * Date: 16/6/29
 * Time: 上午9:37
 */

use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal'],
    'fieldConfig' => [
        'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
        'labelOptions' => ['class' => 'col-lg-1 control-label'],
    ],
]); ?>

<?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>

<?= $form->field($model, 'password')->passwordInput() ?>

    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
        </div>
    </div>

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

Let’s do some debugging. Don’t use the framework’s own Yii::$app->request->post() to get it. Directly enter the controller method program entry var_dump($_POST) to see if there is a value

You have obtained the EntryForm instance $model through $model->load(Yii::$app->request->post()); try printing $model->username

You can try this method:

<code>$model->load(Yii::$app->request->post(), '' );</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn