登录  /  注册
急求tp5 ajax请求 数据一直添加不进数据库
phpcn_u102592
phpcn_u102592 2018-04-08 20:17:33
[PHP讨论组]

<?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>

phpcn_u102592
phpcn_u102592

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号