<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018-4-3
* Time: 15:23
*/
namespace app\index\controller;
use app\common\controller\Base;
use app\common\model\Test as TestModel;
class Test extends Base
{
public function test1()
{
return $this->fetch();
}
public function insert()
{
if (Request::isAjax()){
$data = Request::except('password_confirm','post');
if(TestModel::create($data)){
return ['status'=>1 , 'message'=>'恭喜,注册成功!'];
}else{
return ['status'=>0 , 'message'=>'注册失败!'];
}
}
else{
$this->error("请求类型错误",'register');
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/static/css/bootstrap.css"/>
<script src="/static/js/jquery-3.3.1.min.js"></script>
<script src="/static/js/bootstrap.js"></script>
</head>
<body>
<form class="form-horizontal" method="post" id="testPost">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" name="name" class="form-control" id="inputEmail3" placeholder="name">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" name="password" class="form-control" id="inputPassword3" placeholder="Password">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" name="password_confirm" class="form-control" id="inputPassword4" placeholder="Password_confirm">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" id="testButton">Sign in</button>
</div>
</div>
</form>
<script>
$(function () {
$("#testButton").on('click',function () {
$.ajax({
type:'post',
url:"{:url('insert')}",
data: $('#testPost').serialize(),
dataType: 'json',
success: function (data) {
alert('成功了');
}
})
})
})
</script>
</body>
</html>
豆芽2018-04-09 20:52:15
Add use think\Request at the top
In addition, the insert() method needs to pass in parameters, insert(Requset $request)
{
$data = $request->param();
}