thinkphp3.2 ajax注册

14 篇文章 0 订阅

1.index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<script src="/TpPublic/youlipinM/js/zepto.min.js"></script>
<script src="/TpPublic/youlipinM/js/zhuce.js"></script>
<script src="/TpPublic/youlipinM/js/adaptation.js"></script>
<body>
<form>
  用户名 <input type="text" name="username" id="username" ><br>
  密码 <input type="password" name="pass" id="pass" ><br>
  <input type="button" value="注册" οnclick="checkReg()">
  <div  class="tishi"><b class="close"></b><p class="err"></p></div>
</form>
</body>
<script>
  function  checkReg(){
    var username =$("#username").val();
    var reg = /^\w{3,}$/;
    var pass = $("#pass").val();
    if(username =="" ||!reg.test(username)){
      $('.err').html('您的用户名有误');
      $('.tishi').show();
      return false;
    };
    if(pass==''){
      $('.err').html('密码有误');
      $('.tishi').show();
      return false;
    };
//  使用ajax传用户名密码 到后台控制器
    $.ajax({
      type:"post",
      url:"{:U('Reg/reg')}",
      data:{'username':username,'pass':pass},
      success:function (msg){
        if(msg =='1'){
          alert('注册成功');
          //注册成功跳转至登录页面
          window.location.href = "{:U('Login/login')}";
        }else{
          alert('注册失败');
          //注册失败跳转至注册表单页面
          window.location.href="{:U('Reg/index')}";
        }
      }
    })
  }
</script>
</html>

2.逻辑层   RegController.class.php  

<?php
namespace akszx\Controller;
use Think\Controller;
class RegController extends Controller{
    public function index(){
        $this->display();
    }

    public function reg(){
            //验证用户名唯一性 如果已存在给出提示
            $data['username'] = trim($_POST['username']);
            $find = M()->table('kehu_users')->where($data)->find();
            if(!empty($find)){
               echo 'had';
            }else{
            //插入
                $data['username'] = trim($_POST['username']);
                $data['pass'] = md5($_POST['pass']);
                $res = M()->table('kehu_users')->add($data);
                if ($res){
                    //表示注册成功
                    echo '1';
                }else{
                    //表示注册失败
                    echo '2';
                }
            }
    }
}

学习时收藏积累的,希望对看到的您有所帮助哦!haliluya!God love you!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大帅同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值