zuixian113 2017-04-05 15:46:01 3667次浏览 1条回复 1 0 0

1、TestController控制器

<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
class TestController extends Controller
{
	public $enableCsrfValidation=false;//禁用CSRF验证拦截
	
	public function actionAdd()
	{
		return $this->render('add');
	}
	
	public function actionDoadd()
	{
		$request=Yii::$app->request;
		$id= $request->post('id',1);
		$name= $request->post('name','yiyi');
		return $this->render('doadd',[
			'id'=>$id,
			'name'=>$name,
		]);
	}

        public function actionDoadd2()
	{
		$request=Yii::$app->request;
		$id= $request->get('id',1);
		$name= $request->get('name','yiyi');
		return $this->render('doadd',[
			'id'=>$id,
			'name'=>$name,
		]);
	}
}

2、add.php 视图

<?php
use yii\helpers\Html;
?>
添加表单数据页面!
<br>
<form action="/yii2/basic/web/index.php?r=test/doadd" method="post">
<label>编号</label>
<input type="text" id="id" name="id">
<br>
<label>名称</label>
<input type="text" id="name" name="name">
<input type="submit" value="提交">
</form>

<h3>超链接传值</h3>
<a href="/yii2/basic/web/index.php?r=test/doadd2&id=123&name=zhangsan">点击传值</a>

3、doadd.php视图

<?php
	use yii\helpers\Html;
?>

Test-index!<br>
<?= Html::encode($id) ?> <br>
<?=Html::encode($name)?><br>
<br>
<?=$id?> <br>
<?=$name?><br>
  • 回复于 2018-02-01 22:34 举报

    public $enableCsrfValidation=false;//禁用CSRF验证拦截
    禁用CSRF验证拦截,才能获取到原生html 表单提交的数据,否则会报Unable to verify your data submission. 错误

您需要登录后才可以回复。登录 | 立即注册