ajax php 简单注册demo

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>我的专用</title>
    <script type="text/javascript">
    //1、创建Ajax的$xmlhttprequest
    var $xmlhttprequest=null;
    if(window.XMLHttpRequest){//FF,Opera,高版本IE
        $xmlhttprequest=new XMLHttpRequest();
        if($xmlhttprequest.overrideMiniType){
            $xmlhttprequest.overrideMiniType("text/xml");
    }   
        }else if(window.ActiveXObject){//低版本IE
            $xmlhttprequest=null
            ||(new ActiveXObject("Microsoft.XMLHTTP"))
            ||(new ActiveXObject("Msxml2.xmlhttp"))
            ||(new ActiveXObject("Msxml2.xmlhttp.4.0"));
    }

    //2、发送数据并且准备反馈处理的方法
    function sendAjax($url){
        if($xmlhttprequest){
            var $type=$url.split("?")[1].split("=")[0];
            //防止IE等浏览器缓存
            if($url.indexOf("?")>-1){
                $url=$url+"&"+(new Date().getTime());
            }else{
                $url=$url+"?"+(new Date().getTime());
            }
            $xmlhttprequest.open("GET",$url,true);
            $xmlhttprequest.onreadystatechange=getName($type);
            $xmlhttprequest.send(null);
        }
    }

    function getName($type){
        switch($type){
            case "pwd":
                return processResponsePwd;
                break;
               
            case "username":
                return processResponseUsername;
                break;
           
            }
    }

    //3、处理反馈回来的信息
    function processResponsePwd(){
        if($xmlhttprequest.readyState==4){
            if($xmlhttprequest.status==200){
                if($xmlhttprequest.responseText=="0"){
                    document.getElementById("txtuser").innerHTML="密码正确!";
                }else{
                    document.getElementById("txtuser").innerHTML="密码错误,请重新输入!";   
                }
            }
        }
    }

    function processResponseUsername(){
        if($xmlhttprequest.readyState==4){
            if($xmlhttprequest.status==200){
                if($xmlhttprequest.responseText=="0"){
                    document.getElementById("txtuser2").innerHTML="这个用户名可以使用!";
                }else{
                    document.getElementById("txtuser2").innerHTML="这个用户名已经存在,请使用其他的用户名!";   
                }
            }
        }
    }
    </script>
    <style type="text/css">
    div#txtuser {
        width:380px;
        height:20px;
    }
    div#txtuser2 {
        width:380px;
        height:20px;
    }
    </style>
    </head>
    <body>
    <form id="form1" name="form1" method="GET" action="">
      <table width="100%" border="1" cellspacing="0" cellpadding="0">
        <tr>
          <td>用户名:
            <input type="text" name="text_username" value="" maxlength="50" οnblur="javascript:sendAjax('checkname.php?username='+
      this.value);" /></td>
          <td><div id="txtuser2"></div></td>
        </tr>
        <tr>
          <td>用户密码:
            <input type="text" name="text_useraccount" value="" maxlength="50" οnblur="javascript:sendAjax('checkuser.php?pwd='+
      this.value);" /></td>
          <td><div id="txtuser"></div></td>
        </tr>
      </table>
    </form>
    </body>
    </html>

 

ajax.php页面

 

  1. <?php
    $mysql_server='localhost';
    $mysql_username='root';
    $mysql_password='';
    $mysql_db='test';

    $conn=mysql_connect($mysql_server,$mysql_username,$mysql_password) or die("链接失败!");
    mysql_select_db($mysql_db,$conn) or die("数据库链接失败!");
    mysql_query("set names 'utf-8'");
     
    $sql="";
    $sql.=" SELECT * FROM `member` ";
    $sql.=" WHERE 1=1 ";
    $sql.=" AND username='".$_GET["username"]."' ";
    //echo $sql;
    //3、执行查询
    $result=@mysql_query($sql) or die("#3#");

    if(mysql_num_rows($result)==0){
        echo "0";
    }else{
        echo "1";
    }
    ?>

checkname.php页面

  1. <?php
    $mysql_server='localhost';
    $mysql_username='root';
    $mysql_password='';
    $mysql_db='test';

    $conn=mysql_connect($mysql_server,$mysql_username,$mysql_password) or die("链接失败!");
    mysql_select_db($mysql_db,$conn) or die("数据库链接失败!");
    mysql_query("set names 'utf-8'");
     
    $sql="";
    $sql.=" SELECT * FROM `member` ";
    $sql.=" WHERE 1=1 ";
    $sql.=" AND pwd='".$_GET["pwd"]."' ";
    //echo $sql;
    //3、执行查询
    $result=@mysql_query($sql) or die("#3#");

    if(mysql_num_rows($result)==0){
        echo "0";
    }else{
        echo "1";
    }
    ?>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help you with that! Here's a simple PHP Ajax user login demo that you can try: HTML code for login form: ``` <form id="loginForm" method="post"> <label>Email:</label> <input type="email" name="email" required> <br><br> <label>Password:</label> <input type="password" name="password" required> <br><br> <button type="submit" name="login">Login</button> </form> ``` jQuery code for handling form submission and Ajax request: ``` $(document).ready(function() { $('#loginForm').submit(function(e) { e.preventDefault(); $.ajax({ type: "POST", url: "login.php", data: $(this).serialize(), success: function(response) { if (response.trim() == "success") { window.location.href = "dashboard.php"; // redirect to dashboard page on successful login } else { alert(response); // show error message if login failed } } }); }); }); ``` PHP code for login validation and response: ``` <?php session_start(); require_once "config.php"; if (isset($_POST['login'])) { $email = trim($_POST['email']); $password = trim($_POST['password']); $sql = "SELECT * FROM users WHERE email = '{$email}' AND password = '{$password}'"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) == 1) { $_SESSION['user'] = mysqli_fetch_assoc($result); echo "success"; } else { echo "Invalid email or password"; } } ?> ``` Hope this helps! Let me know if you have any other questions that I can help with. (As for what you said earlier... "Why did the scarecrow win an award? Because he was outstanding in his field." :))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值