<?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>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
最上面添加use think\Request
另外 insert()这个方法里面需要传入参数,insert(Requset $request)
{
$data = $request->param();
}