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!