标签搜索

php+ajax实现登录注册

默然
2021-07-08 / 1 评论 / 239 阅读 / 正在检测是否收录...

登录注册界面

dlzc.PNG
dl.JPG
zc.JPG


<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="utf-8">
  <title>php+ajax实现登录注册</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <style>
    body {
      font: 16px arial, 'Microsoft Yahei', 'Hiragino Sans GB', sans-serif;
    }

    h1 {
      margin: 0;
      color: #3a87ad;
      font-size: 26px;
      text-align: center;
    }

    .content {
      width: 45%;
      margin: 0 auto;

    }

    .content>div {
      margin-top: 200px;
      padding: 20px;
      background: #d9edf7;
      border-radius: 12px;
    }

    .content dl {
      color: #2d6a88;
      line-height: 40px;
      text-align: center;
    }

    .content div div {
      padding-bottom: 20px;
      text-align: center;
    }
  </style>
</head>

<body>
  <div class="content">
    <div>
      <h1>登录</h1>
      <dl>
        账号:<input type="text" name="user" id="user" placeholder="请输入账号">
        </br>
        密码:<input type="text" name="pwd" id="pwd" placeholder="请输入密码">
        </br>
        <input type="button" value="登录" onclick="login()">
      </br>
      <input type="button" value="注册" onclick="addzc()">      
      </dl>
    </div>
  </div>
  <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
  <script>
    function login(){
      var user = $.trim($("#user").val());
      var pwd = $.trim($("#pwd").val());

      if(user == "" || pwd == ""){
        alert("请输入完整!")
        return false;
      }
      $.ajax({
        type : "post",
        url : "login.php",
        data : {
          user : user,
          pwd : pwd
        },
        dataType:"json",
        success : function(data){
          if(data.code == 1){
            alert(data.msg);
            //location.href='./index.html';  跳转页面
          }else if(data.code == 3){
            alert(data.msg)
          }else{
            alert(data.msg)
          }
        }
      })
    }
    function addzc(){
      var user = $.trim($("#user").val());
      var pwd = $.trim($("#pwd").val());
      if(user == "" || pwd == ""){
        alert("请输入完整!")
        return false;
      }
      $.ajax({
        type : "post",
        url : "zc.php",
        data : {
          user : user,
          pwd : pwd
        },
        dataType:"json",
        success : function(data){
          if(data.code == 1){
            alert(data.msg);
            //location.href='./index.html';  跳转页面
          }else if(data.code == 3){
            alert(data.msg)
          }else{
            alert(data.msg)
          }
        }
      })
    }
  </script>
</body>

</html>

登录

<?php
$conn = mysqli_connect(
    'localhost',  /*连接MySQL地址 */
    'ajaxqwer',      /*连接MySQL用户名 */
    'ajaxqwer',  /*连接MySQL密码 */
    'ajaxqwer'  /*连接数据库名称*/
);  
if (!$conn) {
    printf("数据库连接失败", mysqli_connect_error());
    exit;
}

$user = $_POST["user"];
$pwd = $_POST["pwd"];
$sql = "select * from user where user = '$user'";
$result = mysqli_query($conn,$sql);
$rows = mysqli_fetch_array($result);
if($rows){
    if($pwd == $rows["pwd"]){
        echo json_encode(array("code"=>1,"msg"=>"登录成功"));
    }else{
        echo json_encode(array("code"=>2,"msg"=>"密码错误"));        
    }
}else{
    echo json_encode(array("code"=>3,"msg"=>"账号不存在"));
}

?>

注册

<?php

$conn = mysqli_connect(
    'localhost',  /*连接MySQL地址 */
    'ajaxqwer',      /*连接MySQL用户名 */
    'ajaxqwer',  /*连接MySQL密码 */
    'ajaxqwer'  /*连接数据库名称*/
);  
if (!$conn) {
    printf("数据库连接失败", mysqli_connect_error());
    exit;
}

$user = $_POST["user"];
$pwd = $_POST["pwd"];
$sql = "select * from user where user = '$user'";
$result = mysqli_query($conn,$sql);
$rows = mysqli_fetch_array($result);
if($rows){
    echo json_encode(array("code"=>3,"msg"=>"账号已存在"));
}else{
    $addsql = "INSERT INTO user (user,pwd) VALUES ('$user','$pwd')";
    $retval = mysqli_query( $conn, $addsql);
    if(!$retval){
        echo json_encode(array("code"=>2,"msg"=>"注册失败"));
    }else{
        echo json_encode(array("code"=>1,"msg"=>"注册成功"));
    }
}

?>

附件下载

2

评论 (1)

取消
  1. 头像
    fsaf
    Windows 7 · Google Chrome

    风刀霜剑佛is大家

    回复