我定义一个实体 无法获取POST过来的表单数据,代码如下:
1、Action
namespace app\controllers;
use app\models\FormModel;
use Yii;
use yii\web\Controller;
use yii\web\Request;
use yii\web\Response;
class FormController extends Controller {
public $enableCsrfValidation = false;
public function actionIndex()
{
$this->layout=false;
return $this->render("index",[]);
}
public function actionPost()
{
$model=new FormModel();
$model->load($_POST);
$model->attributes=$_POST;
echo json_encode($model->name);
}
}
2、Model
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class FormModel extends ActiveRecord {
public static function tableName()
{
return 'formmodel';
}
public function rules()
{
return [
[['name', 'name'], 'required'],
['id', 'id'],
];
}
}
3、视图
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width"/>
<title>用户登录</title>
<script src="/static/js/vendor/jquery-2.1.3.min.js"></script>
</head>
<body>
<div>
<form id="form1" method="post" action="/form/post">
id:<input type="text" name="id"/><br/>
name:<input type="text" name="name"/><br/>
<input type="button" id="btn" value="sumbit"/>
</form>
</div>
<script type="text/javascript">
$(function () {
$("#btn").click(function () {
$.ajax({
type: 'POST',
url: "/form/post",
data: $("#form1").serialize(),
success: function (data) {
},
dataType: "html"
});
});
});
</script>
</body>
</html>
4、视图对应表SQL
CREATE TABLE `formmodel` (
`id` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
错误:
Exception 'ReflectionException' with message 'Class id does not exist'
in E:\Soft\PHP\yii\yii-basic-app-2.0.3\basic\vendor\yiisoft\yii2\di\Container.php:415
Stack trace:
#0 E:\Soft\PHP\yii\yii-basic-app-2.0.3\basic\vendor\yiisoft\yii2\di\Container.php(415): ReflectionClass->__construct('id')
#1 E:\Soft\PHP\yii\yii-basic-app-2.0.3\basic\vendor\yiisoft\yii2\di\Container.php(358): yii\di\Container->getDependencies('id')
#2 E:\Soft\PHP\yii\yii-basic-app-2.0.3\basic\vendor\yiisoft\yii2\di\Container.php(151): yii\di\Container->build('id', Array, Array)
#3 E:\Soft\PHP\yii\yii-basic-app-2.0.3\basic\vendor\yiisoft\yii2\BaseYii.php(344): yii\di\Container->get('id', Array, Array)
#4 E:\Soft\PHP\yii\yii-basic-app-2.0.3\basic\vendor\yiisoft\yii2\validators\Validator.php(204): yii\BaseYii::createObject(Array)
#5 E:\Soft\PHP\yii\yii-basic-app-2.0.3\basic\vendor\yiisoft\yii2\base\Model.php(422): yii\validators\Validator::createValidator('id', Object(app\models\FormModel), Array, Array)
#6 E:\Soft\PHP\yii\yii-basic-app-2.0.3\basic\vendor\yiisoft\yii2\base\Model.php(386): yii\base\Model->createValidators()
#7 E:\Soft\PHP\yii\yii-basic-app-2.0.3\basic\vendor\yiisoft\yii2\base\Model.php(184): yii\base\Model->getValidators()
#8 E:\Soft\PHP\yii\yii-basic-app-2.0.3\basic\vendor\yiisoft\yii2\base\Model.php(715): yii\base\Model->scenarios()
#9 E:\Soft\PHP\yii\yii-basic-app-2.0.3\basic\vendor\yiisoft\yii2\base\Model.php(659): yii\base\Model->safeAttributes()
#10 E:\Soft\PHP\yii\yii-basic-app-2.0.3\basic\vendor\yiisoft\yii2\base\Component.php(169): yii\base\Model->setAttributes(Array)
#11 E:\Soft\PHP\yii\yii-basic-app-2.0.3\basic\vendor\yiisoft\yii2\db\BaseActiveRecord.php(266): yii\base\Component->__set('attributes', Array)
#12 E:\Soft\PHP\yii\yii-basic-app-2.0.3\basic\controllers\FormController.php(25): yii\db\BaseActiveRecord->__set('attributes', Array)
#13 [internal function]: app\controllers\FormController->actionPost()
表单名称命名不符合yii规范,
改为url: "/form/post",
有错误
1.检查点击是否做ajax提交了
2.后台试着输出POST过去的数据,看看是不是你php代码有问题
$model=new FormModel();这个有东西么?