<tfoot id='oTi33'></tfoot>

    1. <i id='oTi33'><tr id='oTi33'><dt id='oTi33'><q id='oTi33'><span id='oTi33'><b id='oTi33'><form id='oTi33'><ins id='oTi33'></ins><ul id='oTi33'></ul><sub id='oTi33'></sub></form><legend id='oTi33'></legend><bdo id='oTi33'><pre id='oTi33'><center id='oTi33'></center></pre></bdo></b><th id='oTi33'></th></span></q></dt></tr></i><div id='oTi33'><tfoot id='oTi33'></tfoot><dl id='oTi33'><fieldset id='oTi33'></fieldset></dl></div>

      <legend id='oTi33'><style id='oTi33'><dir id='oTi33'><q id='oTi33'></q></dir></style></legend>
    2. <small id='oTi33'></small><noframes id='oTi33'>

          <bdo id='oTi33'></bdo><ul id='oTi33'></ul>

        ajax+php实现用户登录

        一.ajax完成用户名异步检验 html代码: !DOCTYPE htmlhtml lang="en"head meta charset="UTF-8" title用户名是否存在/title/headbodyform action="" method="post" table tr td用户名/td tdinput type="text" name="username" placehol
          <tbody id='vRZN6'></tbody>
      1. <tfoot id='vRZN6'></tfoot>
        <i id='vRZN6'><tr id='vRZN6'><dt id='vRZN6'><q id='vRZN6'><span id='vRZN6'><b id='vRZN6'><form id='vRZN6'><ins id='vRZN6'></ins><ul id='vRZN6'></ul><sub id='vRZN6'></sub></form><legend id='vRZN6'></legend><bdo id='vRZN6'><pre id='vRZN6'><center id='vRZN6'></center></pre></bdo></b><th id='vRZN6'></th></span></q></dt></tr></i><div id='vRZN6'><tfoot id='vRZN6'></tfoot><dl id='vRZN6'><fieldset id='vRZN6'></fieldset></dl></div>
      2. <small id='vRZN6'></small><noframes id='vRZN6'>

            <bdo id='vRZN6'></bdo><ul id='vRZN6'></ul>

          • <legend id='vRZN6'><style id='vRZN6'><dir id='vRZN6'><q id='vRZN6'></q></dir></style></legend>

                  一.ajax完成用户名异步检验
                  html代码:
                  <!DOCTYPE html>
                  <html lang="en">
                  <head>
                      <meta charset="UTF-8">
                      <title>用户名是否存在</title>
                  </head>
                  <body>
                  <form action="" method="post">
                      <table>
                          <tr>
                              <td>用户名</td>
                              <td><input type="text" name="username" placeholder="请输入用户名" class="name" id="name" ></td>
                              <td><span class="note"></span></td>
                          </tr>
                          <tr>
                              <td>密码</td>
                              <td><input type="password" name="password" class="pwd" ></td>
                          </tr>
                          <tr><td><input type="submit" id="check"></td></tr>
                      </table>
                  </form>
                  </body>
                  </html>
                  js代码(当鼠标移开用户名标签时,ajax引擎自动与后台实现异步交互,从而完成验证)
                  <script type="text/javascript">
                      var name=document.getElementById('name');
                      var pwd=document.getElementsByClassName('pwd')[0];
                      document.querySelector('.name').onblur=function () {
                          document.querySelector('.note').innerHTML='验证中……';
                          //1.创建对象
                          var xhr=new XMLHttpRequest();
                          //2.设置请求行(get请求数据写在url后面)
                          xhr.open('post','check.php');
                          //3.设置请求头(get请求可以省略,post不发送数据也可以省略)
                          xhr.setRequestHeader('content-type',"application/x-www-form-urlencoded");
                          //3.5注册回调函数
                          xhr.onload=function () {
                              if(xhr.status==200 && xhr.readyState==4)
                              {
                                  console.log(xhr.responseText);
                                  var data=JSON.parse(xhr.responseText);
                                  console.log(data);
                                  if(data.flag==3) {
                                      document.querySelector('.note').innerHTML = data.msg;
                                  }
                              }
                          };
                          //4.请求主体发送(get请求为空,或者写null,post请求数据写在这里,如果没有数据,直接为空或者写null)
                          //post请求发送数据 写在send中
                          //key=value&key2=value2
                          xhr.send("username="+document.getElementById('name').value);
                      };
                  </script>
                  后台php文件(check_username.php):
                  <?php
                  //print_r($_POST);
                  $flag=0;
                  $msg='failure';
                  $username=isset($_POST['username'])?$_POST['username']:"";
                  $password=isset($_POST['password'])?$_POST['password']:"";
                  
                  if($username==='admin'){
                      $flag=3;
                      $msg='用户名正确';
                  }else {
                      $flag=3;
                      $msg='用户名不存在';
                  }
                  ?>

                  知识点-----AJAX - onreadystatechange 事件

                  当发送一个请求后,客户端需要确定这个请求什么时候会完成,因此,XMLHttpRequest对象提供了onreadystatechange事件机制来捕获请求的状态,继而实现响应。

                   当请求被发送到服务器时,我们需要执行一些基于响应的任务。

                   每当readyState改变时,就会触发onreadystatechange事件。

                   readyState属性存有 XMLHttpRequest 的状态信息。

                   下面是 XMLHttpRequest 对象的三个重要的属性:

                  注意:POST请求不加请求头,数据是传不到后台的

                  二.提交时完成后用户名与密码的验证

                  创建一个后台文件(check_login.php)用来验证用户名与密码

                  新建php文件check_login.php(用户数据这里写死,一般是从数据库查询得到的)

                  $username=isset($_POST['username'])?$_POST['username']:"";
                  $password=isset($_POST['password'])?$_POST['password']:"";
                  if($username=='admin' && $password==123){
                      $flag=1;
                      $msg='登录成功';
                  } else {
                      $flag=2;
                      $msg='密码错误';
                  }
                  
                  $response=[
                      'flag'=>$flag,
                      'msg'=>$msg,
                  ];
                  echo json_encode($response);
                  在原来的登录界面的js脚本里加入点击时的验证
                  document.getElementById('check').onclick=function () {
                          var xhr=new XMLHttpRequest();
                          xhr.open('post','check_login.php');
                          xhr.setRequestHeader('content-type',"application/x-www-form-urlencoded");
                          xhr.onreadystatechange=function () {
                              if(xhr.readyState==4 && xhr.status==200){
                                  var data=JSON.parse(xhr.responseText);
                                  if(data.flag==1) {
                                    alert(data.msg);
                                    console.log(data);
                                  }else if(data.flag==2){
                                      alert(data.msg);
                                      console.log(data);
                                  }
                              }
                          };
                          xhr.send('username='+document.getElementById('name').value+'&password='+pwd.value);
                      }
                  【网站声明】本站部分内容来源于互联网,旨在帮助大家更快的解决问题,如果有图片或者内容侵犯了您的权益,请联系我们删除处理,感谢您的支持!

                  相关文档推荐

                  Uncaught PDOException reveals username and password(未捕获的 PDOException 显示用户名和密码)
                  Check if username exists PDO(检查用户名是否存在 PDO)
                  Laravel 5.2 - How to logout a user from all of his devices(Laravel 5.2 - 如何从用户的所有设备上注销)
                  Elegant way to search an PHP array using a user-defined function(使用用户定义函数搜索 PHP 数组的优雅方式)
                  How to implement service layer in Zend Framework?(Zend Framework中如何实现服务层?)
                  How do I implement Direct Identity based OpenID authentication with Zend OpenID(如何使用 Zend OpenID 实现基于直接身份的 OpenID 身份验证)
                      <i id='CX0uq'><tr id='CX0uq'><dt id='CX0uq'><q id='CX0uq'><span id='CX0uq'><b id='CX0uq'><form id='CX0uq'><ins id='CX0uq'></ins><ul id='CX0uq'></ul><sub id='CX0uq'></sub></form><legend id='CX0uq'></legend><bdo id='CX0uq'><pre id='CX0uq'><center id='CX0uq'></center></pre></bdo></b><th id='CX0uq'></th></span></q></dt></tr></i><div id='CX0uq'><tfoot id='CX0uq'></tfoot><dl id='CX0uq'><fieldset id='CX0uq'></fieldset></dl></div>
                        <tbody id='CX0uq'></tbody>

                          <small id='CX0uq'></small><noframes id='CX0uq'>

                          <tfoot id='CX0uq'></tfoot>
                            <bdo id='CX0uq'></bdo><ul id='CX0uq'></ul>

                          • <legend id='CX0uq'><style id='CX0uq'><dir id='CX0uq'><q id='CX0uq'></q></dir></style></legend>