基于ajax+php+mysql数据库实现用户注册登录(转)

转至https://blog.csdn.net/hjw453321854/article/details/78810979

1.注册登录界面:

form.html

 
  1. <html>

  2. <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>

  3. <link href="form.css" rel="stylesheet"/>

  4. <style>

  5. *{margin:0;padding:0;}

  6.  
  7. </style>

  8. <body>

  9. <div class="bg">

  10. <img src="bg_img.jpg" style="width:100%;height:100%;">

  11. <div class="Forms">

  12. <div class="Forms_box">

  13. <div class="Forms_head">

  14. <a class="a1">快速注册</a>

  15. <a class="a2 active">快速登录</a>

  16. </div>

  17. <div class="submit ">

  18. <form action="signup.php" method="POST" class="Form1" id="Form1" onsubmit="return check()">

  19. <span>账号 : </span><input class="input1" type="username" name="signup_userName" onblur="signupusername()" required placeholder="请输入手机号">

  20. <p class="text1">手机号码有误</p><p class="text2"></p>

  21. <span>密码 : </span><input class="input2" type="password" name="signup_passWord" onblur="signuppassword()" required placeholder="请输入密码">

  22. <p class="text">密码必须由6-12位数字加字母组成</p>

  23. <p class="yz">

  24. <span>验证码 :</span>

  25. <input type ="text" id="text" class="yz_text" required placeholder="请输入验证码">

  26. <input type="button" id="code" onclick="Code()" title='点击更换验证码' >

  27. </p>

  28. <input class="sub1" type="submit" value="立即注册">

  29. </form>

  30. </div>

  31. <div class="submit action">

  32. <form action="login.php" method="POST">

  33. <span>账号 : </span><input class="input3" type="username" name="login_userName" required placeholder="请输入手机号" >

  34. <span>密码 : </span><input class="input4" type="password" name="login_passWord" required placeholder="请输入密码">

  35. <input class="sub2" type="submit" value="立即登录">

  36. </form>

  37. </div>

  38. </div>

  39. </div>

  40. </div>

  41. <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>

  42. <script src="form.js" charset="utf-8"></script>

  43. </body>

  44. </html>

form.css

 
  1. @charset "utf-8;"

  2. body,ul,li,a,p,span{font-family:"微软雅黑"}

  3. a{text-decoration:none;}

  4.  
  5. .bg{position:relative;}

  6. .bg .Forms{width:400px;height:300px;position:absolute;top:50%;left:50%;margin-left:-200px;margin-top:-150px;box-shadow:1px 1px 50px #999;border-radius:10px;background:#fff;}

  7. .bg .Forms .Forms_box{width:400px;height:300px;position:relative;}

  8. .bg .Forms .Forms_box .Forms_head{width:400px;height:50px;line-height:50px;border-bottom:2px solid #ccc;}

  9. .bg .Forms .Forms_box .Forms_head .a1{width:80px;height:40px;line-height:40px;text-align:center;color:#333;font-size:18px;position:absolute;top:10px;left:70px;cursor:pointer;}

  10. .bg .Forms .Forms_box .Forms_head .active{border-bottom:2px solid #19bfe8;}

  11. .bg .Forms .Forms_box .Forms_head .a2{width:80px;height:40px;line-height:40px;text-align:center;color:#333;font-size:18px;position:absolute;top:10px;right:70px;cursor:pointer;}

  12. .bg .Forms .Forms_box .submit{width:260px;height:150px;margin:30px 70px;position:absolute;display:none;top:50px;}

  13. .bg .Forms .Forms_box .submit form input{width:200px;height:30px;line-height:30px;padding-left:3px;font-size:12px;color:#000;outline:none;}

  14. .bg .Forms .Forms_box .submit form .input4{margin-top:25px;}

  15. .bg .Forms .Forms_box .submit form span{font-size:16px;margin-right:10px;}

  16. .bg .Forms .Forms_box .submit form .text1{height:15px;text-align:left;font-size:12px;color:red;margin:5px 0 5px 0;padding-left:55px;opacity: 0}

  17. .bg .Forms .Forms_box .submit form .text2{width:260px;height:15px;text-align:left;font-size:12px;color:red;margin:5px 0 5px 0;padding-left:55px;position:absolute;top:30px;}

  18. .bg .Forms .Forms_box .submit form .text{height:15px;height:15px;text-align:left;font-size:12px;color:#fff;margin-top:5px;padding-left:55px;}

  19. .bg .Forms .Forms_box .submit form .yz{width:260px;height:30px;margin:5px 0 0 -16px;}

  20. .bg .Forms .Forms_box .submit form .yz_text{width:100px;height:30px;line-height:30px;display:inline-block;outline:none;}

  21. .bg .Forms .Forms_box .submit form #code{width:70px;height:30px;line-height:30px;color:green;border:0;font-size:18px;letter-spacing:3px;font-weight:bolder;margin-left:10px;outline:none;}

  22. .bg .Forms .Forms_box .submit .sub1{width:100px;height:30px;margin:20px 0 0 55px;display:block;cursor:pointer;background:#19bfe8;color:#fff;border-radius:5px;border:0px;font-size:14px;}

  23. .bg .Forms .Forms_box .submit .sub2{width:100px;height:30px;margin:20px 0 0 50px;display:block;cursor:pointer;background:#19bfe8;color:#fff;border-radius:5px;border:0px;font-size:14px;}

  24. .bg .Forms .Forms_box .action{display:block;}

form.js

 
  1. $(document).ready(function(){

  2. $(".bg .Forms .Forms_box .Forms_head a").click(function(){

  3. var index=$(this).index();

  4. $(this).addClass("active").siblings().removeClass("active")

  5. $(".bg .Forms .Forms_box div").eq(index+1).addClass("action").siblings().removeClass("action")

  6. })

  7.  
  8. })

  9.  
  10. var code;

  11. var text=document.getElementById('text');

  12. var checkCode =document.getElementById("code");

  13. var Form1=document.getElementById("Form1");

  14. window.onload=Code();

  15. function Code(){

  16. code = "";

  17. var codeLength = 4;

  18. var random = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',

  19. 'S','T','U','V','W','X','Y','Z');

  20. for(var i = 0; i < codeLength; i++) {

  21. var index = Math.floor(Math.random()*36);

  22. code += random[index];

  23. }

  24. checkCode.value = code;

  25. }

  26.  
  27. $(".bg .Forms .Forms_box .submit form .yz_text").blur(function(){

  28. if(text.value.toUpperCase().length <= 0 || text.value.toUpperCase() != code) {

  29. $(".bg .Forms .Forms_box .submit form .yz_text").css("border","1px solid red");

  30. }else {

  31. $(".bg .Forms .Forms_box .submit form .yz_text").css("border","1px solid #19bfe8")

  32. }

  33. })

  34.  
  35. var flag=false;

  36. var usernamereg = /^1(3|4|5|7|8)\d{9}$/;

  37. var passwordreg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$/;

  38. var checkusername = document.getElementById("signup_username");

  39. function signupusername(){

  40. if(!usernamereg.test($(".bg .Forms .Forms_box .submit .input1").val())){

  41. $(".bg .Forms .Forms_box .submit .text1").css("opacity","1")

  42. }else{

  43. $(".bg .Forms .Forms_box .submit .text1").css("opacity","0")

  44. }

  45. if($(".bg .Forms .Forms_box .submit .input1").val().length>0){

  46. var xhr=new XMLHttpRequest();

  47. var data="name="+$(".bg .Forms .Forms_box .submit form .input1").val();

  48. xhr.onreadystatechange=function(){

  49. if (xhr.readyState==4 && xhr.status==200){

  50.  
  51. $(".bg .Forms .Forms_box .submit form .text2").html(xhr.responseText);

  52.  
  53. }

  54. }

  55. xhr.open("POST","check.php",true);

  56. xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");

  57. xhr.send(data);

  58. }

  59. }

  60.  
  61. function signuppassword(){

  62. if(!passwordreg.test($(".bg .Forms .Forms_box .submit .input2").val())){

  63. $(".bg .Forms .Forms_box .submit form .text").css("color","red")

  64. }else{

  65. $(".bg .Forms .Forms_box .submit .input2").css("border","1px solid #19bfe8")

  66. $(".bg .Forms .Forms_box .submit form .text").css("color","#fff")

  67. }

  68. }

  69.  
  70. function check(){

  71. console.log($(".bg .Forms .Forms_box .submit form .text2").text())

  72. if(!usernamereg.test($(".bg .Forms .Forms_box .submit .input1").val()) || !passwordreg.test($(".bg .Forms .Forms_box .submit .input2").val())){

  73. return false;

  74. }else if(text.value.toUpperCase().length <= 0 || text.value.toUpperCase() != code){

  75. return false;

  76. }else if($(".bg .Forms .Forms_box .submit form .text2").text() !=" "){

  77. return false;

  78. }else{

  79. return true;

  80. }

  81. }

检查手机号是否注册check.php

 
  1. <?php

  2. $con = mysqli_connect('localhost', 'username', 'password');

  3. if(! $con )

  4. {

  5. die('连接失败: ' . mysqli_error($con));

  6. }

  7. mysqli_select_db($con,'bdm256727651_db');

  8. $username = "select userName FROM Forms where userName = '$_POST[name]' ";

  9. $result = mysqli_query($con,$username );

  10. $rows=mysqli_num_rows($result);

  11. if($rows){

  12. echo "手机号已注册";

  13. exit;

  14. }else{

  15. echo " ";

  16. exit;

  17. }

  18.  
  19. mysqli_close($con)

  20. ?>


 

验证成功保存到数据库signup.php

 
  1. <?php

  2. $con = mysqli_connect('localhost', 'username', 'password');

  3. if(! $con )

  4. {

  5. die('连接失败: ' . mysqli_error($con));

  6. }

  7. mysqli_select_db($con,'bdm256727651_db');

  8. $sql=" INSERT INTO Forms (userName, passWord)

  9. VALUES('$_POST[signup_userName]','$_POST[signup_passWord]') ";

  10. mysqli_query($con,$sql);

  11. echo "<script>window.location.href='signup.html';</script>";

  12. mysqli_close($con)

  13. ?>

注册成功返回页面

 
  1. <!doctype html>

  2. <html lang="en">

  3. <head>

  4. <meta charset="UTF-8">

  5. <title>注册成功</title>

  6. </head>

  7. <body>

  8. 注册成功,请立即登录。

  9. </body>

  10. </html>

登录验证用户名密码login.php

 
  1. <?php

  2.  
  3. $con = mysqli_connect('localhost', 'username', 'password');

  4. if(! $con )

  5. {

  6. die('连接失败: ' . mysqli_error($con));

  7. }

  8.  
  9. mysqli_select_db($con,'bdm256727651_db');

  10. $sql = "select userName,passWord FROM Forms where userName = '$_POST[login_userName]' and passWord='$_POST[login_passWord]'";

  11. $result = mysqli_query($con,$sql );

  12. $rows=mysqli_num_rows($result);

  13.  
  14. if($rows){

  15. echo "<script>window.location.href='login.html';</script>";

  16. exit;

  17. }else{

  18. echo "用户名或密码错误";

  19. echo "

  20. <script>

  21. setTimeout(function(){window.location.href='form.html';},3000);

  22. </script>";

  23. }

  24.  
  25. mysqli_close($con)

  26.  
  27. ?>

登录成功返回页面login.html

 
  1. <!doctype html>

  2. <html lang="en">

  3. <head>

  4. <meta charset="UTF-8">

  5. <title>登陆成功</title>

  6. </head>

  7. <body>

  8. 欢迎光临,登录成功!!!

  9. </body>

  10. </html>

数据库表界面

文章源代码下载:https://github.com/jwhuang59/demo/tree/master/form

猜你喜欢

转载自blog.csdn.net/qq_40795166/article/details/82386698