search

Home  >  Q&A  >  body text

Urgent request for tp5 ajax request. Data cannot be added to the database.

Question: The data has not been added to the database. Please give the management an answer. Thank you


#<!--Head-->

{include file="public:header" /}
<!--Navigation-->
{include file="public:nav" /}

<div class="row">
<!--Page-->
<div class="col-md-8">
" <div class="page-header" >
                                                                                            
                                                                                                                                                           " class="col-sm-2 control-label">Username:</label>
            <div class="col-sm-10">
                                                           " name="name" class="form-control" id="inputEmail1" placeholder="UserName">
                                                                                                     form-group">
-10"> #           </div>
           <div class="form-group">
               <label for="inputEmail3" class="col-sm-2 control-label">手机号:</label>
               <div class="col-sm-10">
                   <input type="text" name="mobile" class="form-control" id="inputEmail3" placeholder="Mobile">
               </div>
           </div>
           <div class="form-group">
               <label for="inputPassword4" class="col-sm-2 control-label">密码:</label>
               <div class="col-sm-10">
                   <input type="password" name="password" class="form-control" id="inputPassword4" placeholder="Password">
               </div>
           </div>
           <div class="form-group">
               <label for="inputPassword5" class="col-sm-2 control-label">确认密码:</label>
               <div class="col-sm-10">
                   <input type="password" name="password_confirm" class="form-control" id="inputPassword5" placeholder="Password Confirm">
               </div>
           </div>
           <div class="form-group">
               <div class="col-sm-offset-2 col-sm-10">
                   <button type="submit" class="btn btn-primary" id="register">注册</button>
               </div>
           </div>
       </form>

   </div>
   <script>
       $(function(){
           $("#register").on('click',function () {
               // alert($('#login').serialize());
               $.ajax({
                   type:'post',
                   url: "{:url('index/user/insert')}",
                   data: $('#login').serialize(),
                   dataType:'json',
                   success:function (data) {
                       alert('成功了');
                   }
               })
           })
       })
   </script>

   {include file="public:right" /}
</div>
<!--Tail-->
{include file="public:footer" /}


<?php

namespace app\ index\controller;

use app\common\controller\Base;
use app\common\model\User as UserModel;
use think\facade\Request;
class User extends Base
{
public function register()
{
$this->assign('title','User registration');
return $this->fetch();
}

Processing the registration information submitted by the user
Public function insert()
{
//Use the model to create data
//Get the data submitted by the user through the form
if(Request::isAjax()){
$data = Request::except('password_confirm','post');
if(UserModel::create($data))
{
                   return ['status'=>1,'message'=>'Congratulations, registration is successful'];
                                                                                                                                                                                                                     return ['status'=> =>'Registration failed, please check'];
        }
            }else{
              $this->error("Request type error",'register');
      }
}
}


Question: The data has not been added to the database. Please give me the management answer. Thank you

一米阳光一米阳光2190 days ago1266

reply all(2)I'll reply

  • 秋香姐家的小书童

    秋香姐家的小书童2019-01-07 09:09:57

    Open the browser console to see what AJAX returns

    reply
    0
  • Cancelreply