php+ajax 登錄注冊頁面


主要是登錄注冊功能,前端后台驗證沒有什么,這個大家可以自己加上去,比如過濾啊,正則啊等

還是先放圖吧

這是登錄及注冊界面  點擊注冊切換到注冊界面,點擊登錄切換到登錄界面

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script>
        <style type="text/css">
        body,html{
            padding: 0px;
            margin: 0px;
        }
        .header {
            width: 100%;
            padding: 0px;
            margin: 0px;
            background-color: #699C03;
        }

        .header-nav {
            min-width: 1170px;
          
            height: 50px;
            line-height: 50px;
        }

        .header-nav-left {
            float: left;
            padding-left: 150px;
               margin-top: 10px;
        }

        .header-nav-left h3 {
            font-size: 18px;
            line-height: 25px;
            margin: 0px;
            color: #fff;
        }

        .header-nav-right {
            float: right;
            width: 59.9999%;
            position: relative;
            height: 50px;
        }

        .header-nav-right ul {
            padding: 0px;
            margin: 0px;
            list-style: none;
            position: absolute;
            right: 25px;
        }

        .header-nav-right ul li {
            float: left;
            margin-left: 5px;
            color: #fff;
        }

        .header-nav-right ul li a {
            color: #fff;
            text-decoration: none;
            
         }
         .user{
             min-width:980px;
             text-align: center;
             margin: 0px;
             padding: 0px;
             position: relative;
         }
         .user-list{
             width:470px;
             height: 250px;
             padding: 15px;
             margin: 20px auto;
             position: relative;
             border: 1px solid #DBDBDB;
         }
         .user-list ul{
             list-style: none;
             width: 100%;
             overflow: hidden;
             height: 250px;
             position: relative;
             padding: 10px 0px;
             margin: 0px;
         }
         .user-list ul li{
             width: 100%;
             list-style: none;
             margin-bottom:15px;
         }
          .user-list ul li i{
              color: red;
          }
         .user-list ul li label{
             display: inline-block;
             width:12%;
             text-align: right;
             font-size:16px;
             margin-right: 10px;
             font-family: "微軟雅黑";
             
         }
         .user-list ul li input{
             display: inline-block;
             border: 1px solid #DBDBDB;
             border-radius:4px;
             -webkit-border-radius: 4px;
             -moz-border-radius: 4px;
             -ms-border-radius:4px;
             height: 30px;
             padding: 2px 5px;
             vertical-align:middle; 
         }
         .buttons{
             width: 100%;
             height:35px;
             padding: 5px 10px;
             position: absolute;
             bottom: 40px;
             left: 0px;
             right: 0px;
         }
         .openbutton,.onbutton{
             display: inline-block;
             width: 40%;
         }
        .buttons button{
            width: 90%;
            color: #fff;
            padding: 5px 10px;
            background-color: #4CB0F9;
            border: 1px solid #4CB0F9;
            border-radius: 4px;
            vertical-align: middle;
            height: 35px;
            line-height: 25px;
            cursor:pointer;
        }
        #form2{
            display: none;
        }
        </style>
    </head>
    <body>
        <div class="header">
            <div class="header-nav">
                <div class="header-nav-left">
                    <h3>網站后台管理</h3>
                </div>
                <div class="header-nav-right">
                    <ul>
                        <li><span>歡迎到來到網站后台</span></li>
                        <li><a href="" onclick="window.location.reload()">主菜單</a></li>
                        <li><a href="">網站首頁</a></li>
                       
                        <li><a href="Index.html">后台主頁</a></li>
                        <li><a href="">內容管理</a></li>
                        <li><a href="">退出</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="user">
            <div class="user-list">
                <form id="form1" action="" method="post">
                    <ul>
                        <li><label>用戶名</label><input type="text" name="username" id="username" placeholder="手機號碼或郵箱"/></li>
                        <li><label>密碼</label><input type="password" name="pass" id="pass" placeholder="6-15位密碼" /></li>
                        <div class="buttons">
                            <a class="onbutton"><button type="submit" id="onsubmit">登錄</button></a>
                            <a class="openbutton"><button type="submit" id="getform2">注冊</button></a>
                        </div>
                    </ul>    
                </form>
                <form id="form2" action="" method="post">
                    <ul>
                        <li><label>用戶名</label><input type="text" name="username" id="getusername" placeholder="手機號碼或郵箱"/><i>*</i></li>
                        <li><label>密碼</label><input type="password" name="pass" id="getpass" placeholder="6-15位密碼" /><i>*</i></li>
                        <div class="buttons">
                            <a class="getbutton"><button type="submit" id="getsubmit">提交注冊信息</button></a>
                            <p>已有賬號立即<a href="#" id="getform1">登錄</a></p>
                        </div>
                    </ul>    
                </form>
            </div>
        </div>
        <script type="text/javascript">
        $(function(){
            $(".onbutton #onsubmit").on('click',function(){
             var $username = $("#username").val(),
                 $pass  = $("#pass").val();
                if($username=='' || $pass==''){
                    alert("用戶名及密碼不能為空");
                    return false;
                }else{
                    var datas={
                        username:$username,
                        pass:$pass
                    };
                    $.ajax({
                        url:'user.php',
                        type:'post',
                        dataType:'json',
                        data:datas,
                        success:function(result){
                            if(result=='nameerror'){
                                alert('用戶名不存在');
                            }else if(result=="passerror"){
                                alert("密碼錯誤");
                            }else{
                                window.location.href = 'http://www.sogou.com';//登錄成功跳轉
                            }
                        },
                        error:function(){
                            alert('false');
                        }
                    })
                }return false;
            })
        $("#getsubmit").click(function(){
            var $username = $("#getusername").val(),
                     $pass  = $("#getpass").val();
                     if($username=='' || $pass==''){
                    alert("用戶名及密碼不能為空");
                    return false;
                    }else{
                        var datas={
                            username:$username,
                            pass:$pass
                        }
                        $.ajax({
                            url:'adduser.php',
                            type:'post',
                            data:datas,
                            dataType:'json',
                            success:function(reslut){
                                if(reslut=="repeat"){
                                    alert("該用戶名已存在");
                                }else if(reslut=='success'){
                                    alert("注冊成功");
                                    //注冊成功后立即切換至登錄表單,並記住用戶名及密碼;
                                    $("#form2").hide();
                                    $("#form1").show();
                                    $("#username").val($username);
                                    $("#pass").val($pass);
                                }
                                else{
                                    alert('false');
                                }
                            }
                        })
                    }return false;
        })
        })
        </script>
        <script type="text/javascript">
            document.getElementById('getform2').onclick = function(){
                document.getElementById('form1').style.display= 'none';
                document.getElementById('form2').style.display= 'block';
                return false;
            }
        document.getElementById('getform1').onclick = function(){
                document.getElementById('form2').style.display= 'none';
                document.getElementById('form1').style.display= 'block';
                return false;
            }
       
        </script>
    </body>
</html>

以上是前端代碼

下面貼后台代碼  ,登錄

<?php
    @header("content-type:text/html;charset=utf8");
    $conne = mysql_connect("localhost","root","root")or die("數據庫用戶名密碼錯誤".@mysql_error());
    $select =mysql_select_db("test",$conne);
    $utf = mysql_query("set names utf8");
    $username=$_REQUEST['username'];
    $pass = $_REQUEST['pass'];
    $arrays = array(array('one'=>'nameerror','two'=>'passerror'));
    $sqlname = mysql_query("select count(*) from user where username= '$username'");
    $row = mysql_fetch_row($sqlname);
    $num = $row[0];
    //查看用戶是否存在
    if(!$num){
        echo json_encode($arrays[0]['one']);//不存在返回nameerror        
    }else{
        $sqlpass = mysql_query("select pass from user where username='$username'");//返回一個句柄;
        $passarray = mysql_fetch_row($sqlpass);//獲得一個只有一行數據的數族
        $passval = $passarray[0];//這里才是對應用戶的密碼
        //判斷該用戶的密碼是否正確
        if($passval!=$pass){
            echo json_encode($arrays[0]['two']);//不正確返回passerror
        }
    }
?>

注冊

<?php
@header("content-type:text/html;charset=uft8");
$conne = mysql_connect("localhost","root","root")or die("數據庫用戶名或密碼錯誤".@mysql_error());
$select = mysql_select_db("test",$conne)or die("數據庫用戶名或密碼錯誤".@mysql_error());
$utf = mysql_query("set names utf8");
$username= $_REQUEST['username'];
$pass = $_REQUEST['pass'];
$arrays = array(array("one"=>'repeat','two'=>'success'));
$sql  = mysql_query("select count(*) from user where username='$username'");
$row = mysql_fetch_row($sql);
$num = $row[0];
//判斷用戶名是否已經被注冊了
if($num == 1){
    echo json_encode($arrays[0]['one']);//被注冊返回repeat
}else{
    mysql_query("insert into user(username,pass) values('$username','$pass')");
    echo json_encode($arrays[0]['two']);
}
?>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM