帖子
帖子
用户
博客
课程
12下一页
返回列表 发新帖
显示全部楼层
34
帖子
0
勋章
52
Y币

PHP+MySQL实现前台的登陆注册。

[复制链接]
发表于 2016-3-10 16:24:33
前台的注册页面

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <style>
    html,
    body {
        height: 100%;
    }

    .row {
        position: relative;
        box-sizing: border-box;
        width: auto;
        height: 70px;
        margin-left: 32px;
        margin-right: 32px;
        padding-top: 40px;
        border-bottom: 1px solid #888;
    }

    .input {
        width: 100%;
        height: 20px;
        border: none;
        font-size: 20px;
    }

    .button {
        position: relative;
        margin-left: 32px;
        margin-right: 32px;
        margin-top: 32px;
        width: auto;
        height: 50px;
        background-color: #e3007f;
        color: #fff;
        font-size: 24px;
        line-height: 50px;
        text-align: center;
        border-radius: 8px;
    }

    .highlight {
        opacity: 0.7;
    }
    </style>
</head>

<body>
    <div class="row">
        <input id="userName" class="input" type="text" placeholder="用户名">
    </div>
    <div class="row">
        <input id="password" class="input" type="password" placeholder="密码">
    </div>
    <div class="row">
        <input id="phone" class="input" type="phone" placeholder="手机号">
    </div>
    <div class="button" tapmode="highlight">注册</div>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/APICloud-rest.js"></script>
<script type="text/javascript" src="../../script/SHA1.js"></script>
<script type="text/javascript">
apiready = function() {

};

下面是从php接口取出数据并判断。

function fnRegister() {
    var uname = $api.byId('userName').value,
        pwd = $api.byId('password').value;
        phone = $api.byId('phone').value;

        api.ajax({
            url:'http://192.168.0.117/APIcloud-PHP/PHP/register.php',//如果地址访问不到会请求出错,请填写自己的接口地址
            method:'post',
            cache:'false',
            timeout:5,
            dataTpye:'json',
            data:{
                    values:{
                                    uname: uname,
                            pwd: pwd,
                            phone: phone
                    }
            }
            },function(ret,err){
                   if (ret.status==1 && ret.num<1) {//各种条件都符合标准的话,注册成功。
                alert(ret.msg);

                api.execScript({  将注册成功的用户名传到登陆页面。
                    name: 'login',
                    frameName: 'login_frame',
                    script: 'fnSetUserName("' + ret.uname + '");'
                });

                api.closeWin();
            }else if(ret.num>=1) {//   num 是根据你注册的时候输入的用户名,在后台查询有没有同样的用户名,如果从后台传过来的num值大于等于一是用户名已存在。
                alert(ret.msg);
            }else if(ret.status==2){//   status是后台判断用户填写的注册信息的完整性,如果有信息没有填写会提示“用户信息不完整”。
                    alert(ret.msg);
            }
        });
};
</script>

</html>




注册的php接口

<?php
header("content-type:text/html;charset=utf-8");
$dsn = "mysql:host=localhost;dbname=apicloud";
$user = "root";
$pwd = "";
$pdo = new PDO($dsn,$user,$pwd,array(PDO::ATTR_PERSISTENT=>TRUE));
$pdo->exec("set names utf8");

        $uname = trim($_POST['uname']);
        $pwd = md5($_POST['pwd']);
        $phone = trim($_POST['phone']);
        if($uname==""||$pwd==""||$phone==""){
                $msg[] = "用户名信息不完整";
                $status[] = "2";
                $list['msg'] = $msg;
                $list['status'] = $status;

                echo json_encode($list);
                exit;
        }

        $data = "select * from uname where uname='$uname'";
        $res = $pdo->prepare($data);
        //$res->exec();
        $res->execute();
        $num = $res->rowCount();
        if($num>=1){
                $msg[] = "用户名已存在,请更换用户名";
                $status[] = "1";
                $list['msg'] = $msg;
                $list['num'] = $num;
                $list['status'] = $status;

                echo json_encode($list);
                exit;
        }

        $sql = "insert into uname (uname, pwd, phone) values ('$uname', '$pwd', '$phone')";
        $result=$pdo->query($sql);
        if($result){
                $status[] = "1";
                $msg[] = "注册成功";
                $list['status'] = $status;
                $list['msg'] = $msg;
                $list['data'] = $result;
                $list['uname'] = $uname;
                $list['num'] = $num;
                echo json_encode($list);
        }else{
                $status[] = "0";
                $msg[] = "注册失败,请重试";
                $list['status'] = $status;
                $list['msg'] = $msg;
                echo json_encode($list);
        }

?>


34
帖子
0
勋章
52
Y币
写的不好的地方大家提出来,共同进步。
1
帖子
0
勋章
841
Y币
感谢分享!Q
2
帖子
0
勋章
56
Y币
求解 我在阿里云上 就是insert语句一直无法执行成功
34
帖子
0
勋章
52
Y币
vantiboolean 发表于 2016-3-16 06:26
求解 我在阿里云上 就是insert语句一直无法执行成功

看一下代码。
2
帖子
0
勋章
166
Y币
简单明了,非常适合我看,谢谢
1
帖子
0
勋章
5
Y币
请问我在iOS中一直报数据格式错误是什么原因呢?安卓上没有这个问题
34
帖子
0
勋章
52
Y币
Gee_Jane 发表于 2016-3-21 01:07
请问我在iOS中一直报数据格式错误是什么原因呢?安卓上没有这个问题

ios的我也没试过。
97
帖子
0
勋章
576
Y币
非常好,像您学习。
34
帖子
0
勋章
52
Y币
不错哦
12下一页
您需要登录后才可以回帖 登录

本版积分规则