主页 > 文章教程 > 织梦Cms教程 > 织梦实现ajax用户注册功能

织梦实现ajax用户注册功能

139 2023-03-01
介绍织梦实现ajax用户注册功能前,我们先看一下注册表单的样式,如下图。
注册表单


-------------------------------html代码-----------------------------------

//表单提交时先走Doreg() js函数。

<form onsubmit="return Doreg();">
    <input type="hidden" name="fmdo" value="reg">
    <h3>注册<p class="status"></p></h3>
    <p class="user_name">
        <input id="user_name" type="text" name="user_name" placeholder="输入英文登录名">
    </p>
    <p class="user_pass">
        <input id="user_pass" type="password" name="user_pass" placeholder="密码最小长度为6">
    </p>
    <p class="user_pass2">
        <input type="password" id="user_pass2" name="user_pass2" placeholder="再次输入密码">
    </p>
    <p class="user_email">
        <input id="user_email" type="email" name="user_email" placeholder="输入常用邮箱">
    </p>
    <p class="vdcode_reg">
        <input type="text" id="vdcode_reg" name="vdcode_reg" placeholder="验证码">
        <img id="vdcode_reg" onclick="this.src=this.src+'?'" alt="看不清?点击更换" src="/include/vdimgck.php">
    </p>
    <p>
        <input type="submit" value="注册" name="submit">
    </p>
</form>

-------------------------------js代码-----------------------------------
function Doreg(){
    var fmdo = $("input[name='fmdo']").val();
    var user_name = $("input[name='user_name']").val();
    var user_pass = $("input[name='user_pass']").val();
    var user_pass2 = $("input[name='user_pass2']").val();
    var user_email = $("input[name='user_email']").val();
    var vdcode_reg = $("input[name='vdcode_reg']").val();
    
    var errmsg = $("<label class='error'></label>");  //创建一个label元素作为存放错误信息容器
    var zyw = /^(([a-zA-Z]))[a-zA-Z]*$/;  //正则表达式,判断只能是英文
    var emailyz = /w+[@]{1}w+[.]w+/;  //正则表达式,判断邮箱格式是否正确
    
    //判断数据是否合法,没问题再提交给后台
    if(user_name==''){
        //拼接错误提示信息并放到触发的输入框
        errmsg.html("请"+$("#user_name").attr("placeholder")).attr("id","username-error").appendTo(".user_name");
        return false
    }else if(!zyw.test(user_name)){
        errmsg.html("登录名只能是英文").attr("id","username-error").appendTo(".user_name");
        return false
    }else if(user_name.length < 3){
        errmsg.html("登录名不能小于3位").attr("id","username-error").appendTo(".user_name");
        return false
    }else if(user_pass==''){
        $(".error").html("");
        errmsg.html("请输入密码").attr("id","password-error").appendTo(".user_pass");
        return false
    }else if(user_pass.length < 6){
        $(".error").html("");
        errmsg.html("密码不能小于6位").attr("id","password-error").appendTo(".user_pass");
        return false
    }else if(user_pass2==''){
        $(".error").html("");
        errmsg.html("请"+$("#user_pass2").attr("placeholder")).attr("id","password2-error").appendTo(".user_pass2");
        return false
    }else if(user_pass2!=user_pass){
        $(".error").html("");
        errmsg.html("两次输入密码不一致").attr("id","password2-error").appendTo(".user_pass2");
        return false
    }else if(user_email==''){
        $(".error").html("");
        errmsg.html("请"+$("#user_email").attr("placeholder")).attr("id","email-error").appendTo(".user_email");
        return false
    }else if(vdcode_reg==''){
        $(".error").html("");
        errmsg.html("请输入"+$("#vdcode").attr("placeholder")).attr("id","vdcode-error").css({right:'95px'}).appendTo(".vdcode_reg");
        return false
    }else{
        $(".error").html("");
        $.ajax({
            type: 'post',
            url: '/ajax_login.php',
            data: {
                fmdo: fmdo,
                step: '1',
                myset: 'ajax_reg',
                dopost: 'ajax_reg',
                user_name: user_name,
                user_pass: user_pass,
                user_pass2: user_pass2,
                user_email: user_email,
                vdcode_reg: vdcode_reg,
            },
            dataType: "json",
            beforeSend: function () {
                // 禁用按钮防止重复提交
                $(".status").html("提交中...");
                $(".submit").attr({ disabled: "disabled" });
            },
            success: function(reg){
                if(reg.son=="ok"){
                    $(".status").html("<i>注册成功并已登录,正在为您刷新...</i>");
                    //设置定时器一秒后刷新页面
                    setTimeout(function(){
                        location.reload()
                    }, 1000)
                    return false
                }else if(reg.son=="err"){
                    $(".status").html("");
                    if(reg.foid=="vdcode_reg"){
                        $(".error").html("");
                        errmsg.html(reg.msg).attr("id",reg.foid+"-error").css({right:'95px'}).appendTo("."+reg.foid);
                    }else{
                        $(".error").html("");
                        errmsg.html(reg.msg).attr("id",reg.foid+"-error").appendTo("."+reg.foid);
                    }
                    setTimeout(function(){
                        $(".submit").removeAttr("disabled");
                    }, 2000)
                    $("#"+reg.foid).focus();
                    return false
                }
            }
        });
        return false
    }
}

-------------------------------php代码-----------------------------------
require_once(DEDEMEMBER.'/config.php');

if($fmdo=='reg'){
    if($step == 1){
        //N表示关闭了新用户注册
        if($cfg_mb_allowreg=='N'){
            //如果$myset=='ajax_reg'为TRUE,表示是ajax请求,给它返回json数据
            if($myset=='ajax_reg'){
                $arr = array("son"=>"err","msg"=>"系统关闭了新用户注册","foid"=>"user_name");
                echo json_encode($arr);
                exit;
            }
            ShowMsg('系统关闭了新用户注册!', '/i/');
            exit();
        }
        
        if(!isset($dopost)) $dopost = '';
        //$step非数字部分转为空
        $step = empty($step)? 1 : intval(preg_replace("/[^d]/", '', $step));
        if($dopost=='ajax_reg'){
            $mtype = '个人';
            $userid = trim($user_name);
            $user_uname = $userid;
            $pwd = trim($user_pass);
            $pwdc = trim($user_pass2);
            //该函数检查准备注册的用户名是否合法,乱码字符和系统关键词等不允许作为用户名
            $rs = CheckUserID($userid, '用户名');
            
            if($rs != 'ok'){
                if($myset=='ajax_reg'){
                    $arr = array("son"=>"err","msg"=>$rs,"foid"=>"user_name");
                    echo json_encode($arr);
                    exit;
                }
                ShowMsg($rs, '-1');
                exit();
            }
            
            if(strlen($userid) > 20 || strlen($user_uname) > 36){
                if($myset=='ajax_reg'){
                    $arr = array("son"=>"err","msg"=>"登录名不能大于20位","foid"=>"user_name");
                    echo json_encode($arr);
                    exit;
                }
                ShowMsg('你的用户名或用户笔名过长,不允许注册!', '-1');
                exit();
            }
            
            if(strlen($userid) < $cfg_mb_idmin){
                if($myset=='ajax_reg'){
                    $arr = array("son"=>"err","msg"=>"登录名不能小于".$cfg_mb_idmin."位","foid"=>"user_name");
                    echo json_encode($arr);
                    exit;
                }
                ShowMsg("你的用户名过短,不允许注册!","-1");
                exit();
            }

            //检测用户名是否存在
            $row = $dsql->GetOne("SELECT mid FROM `dede_member` WHERE userid LIKE '$userid' ");
            if(is_array($row)){
                if($myset=='ajax_reg'){
                    $arr = array("son"=>"err","msg"=>"登录名已存在","foid"=>"user_name");
                    echo json_encode($arr);
                    exit;
                }
                ShowMsg("你指定的用户名 {$user_uname} 已存在,请使用别的用户名!", "-1");
                exit();
            }    
            
            if(strlen($pwd) < $cfg_mb_pwdmin){
                if($myset=='ajax_reg'){
                    $arr = array("son"=>"err","msg"=>"密码不能小于".$cfg_mb_pwdmin."位","foid"=>"user_pass");
                    echo json_encode($arr);
                    exit;
                }
                ShowMsg("你的用户名或密码过短,不允许注册!","-1");
                exit();
            }
            //获取session中的验证码,并跟用户输入的验证码进行比较,看是否一致。
            $svali = GetCkVdValue();
            if(preg_match("/1/", $safe_gdopen)){
                if(strtolower($vdcode_reg)!=$svali || $svali=='')
                {
                    ResetVdValue();
                    if($myset=='ajax_reg'){
                        $arr = array("son"=>"err","msg"=>"验证码错误","foid"=>"vdcode_reg");
                        echo json_encode($arr);
                        exit;
                    }
                    ShowMsg('验证码错误!', '-1');
                    exit();
                }
            }
            
            //给会员表的其它参数赋默认值
            $dfscores = 0;
            $dfmoney = 0;
            $dfrank = $dsql->GetOne("SELECT money,scores FROM `dede_arcrank` WHERE rank='10' ");
            if(is_array($dfrank))
            {
                $dfmoney = $dfrank['money'];
                $dfscores = $dfrank['scores'];
            }
            $jointime = time();
            $logintime = time();
            $joinip = GetIP();
            $loginip = GetIP();
            $pwd = md5($pwd);
            $mtype = RemoveXSS(HtmlReplace($mtype,1));
            $safeanswer = HtmlReplace($safeanswer);
            $safequestion = HtmlReplace($safequestion);
            
            $spaceSta = ($cfg_mb_spacesta < 0 ? $cfg_mb_spacesta : 0);
            
            //把用户信息插入dede_member数据表
            $inQuery = "INSERT INTO `dede_member` (`mtype` ,`userid` ,`pwd` ,`uname` ,`sex` ,`rank` ,`money` ,`email` ,`scores` ,
            `matt`, `spacesta` ,`face`,`safequestion`,`safeanswer` ,`jointime` ,`joinip` ,`logintime` ,`loginip` )
           VALUES ('$mtype','$userid','$pwd','$user_uname','$sex','10','$dfmoney','$user_email','$dfscores',
           '0','$spaceSta','','$safequestion','$safeanswer','$jointime','$joinip','$logintime','$loginip'); ";
            if($dsql->ExecuteNoneQuery($inQuery)){
                //模拟用户登录
                $cfg_ml = new MemberLogin(7*3600);
                $rs = $cfg_ml->CheckUser($userid, $user_pass);
                
                if($myset=='ajax_reg'){
                    $arr = array("son"=>"ok","msg"=>"注册成功并已登录","foid"=>"user_name");
                    echo json_encode($arr);
                    exit;
                }
                ShowMsg("注册成功并已登录,点击确定后返回注册前的页面","-1");
                exit;
            } else {
                if($myset=='ajax_reg'){
                    $arr = array("son"=>"err1","msg"=>"注册失败 请联系管理员","foid"=>"user_name");
                    echo json_encode($arr);
                    exit;
                }
                ShowMsg("注册失败,请检查资料是否有误或与管理员联系!", "-1");
                exit();
            }
        }
    }
}

热门文章