首頁 >後端開發 >php教程 >YII框架接收不到post参数?

YII框架接收不到post参数?

WBOY
WBOY原創
2016-07-06 13:51:532292瀏覽

先上代码 错误肯定就在这两个里边 但是不知道怎么解决 附网址: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>

回复内容:

先上代码 错误肯定就在这两个里边 但是不知道怎么解决 附网址: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>

做个调试看看,你先别用框架自带的Yii::$app->request->post()获取,直接在控制器方法程序入口var_dump($_POST)看看有没有值

通过$model->load(Yii::$app->request->post())就已经获取到了EntryForm的实例$model;打印$model->username试试

你可以尝试这种方法:

<code>$model->load(Yii::$app->request->post(), '' );</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn