doutuosai3504 2019-05-15 03:57
浏览 284
已采纳

匹配在数据库中获取的用户名和密码 - Jquery [复制]

This question already has an answer here:

I need to match my username and password that is fetch in the database. right now the problem is I cannot match the username and password like the image you see below:

enter image description here

here is my code below:

HTML

    <body>  
          <div class="container box">  
           <div class="form-group">  
            <h3 align="center">Live Username Available or not By using PHP Ajax Jquery</h3><br />  
            <label>Enter Username</label>  
            <input type="text" name="username" id="username" class="form-control" />
            <input type="password" name="password" id="password" class="form-control" />


            <span id="availability"></span>
            <br /><br />
            <button type="button" name="register" class="btn btn-info" id="register" disabled>Register</button>
            <br />
           </div>  
           <br />  
           <br />  
          </div>  
  </body>  

jQuery script - this is where the match availability will show after the username and password is match or mismatched

<script>  
 $(document).ready(function(){  
  $('#username, #password').blur(function(){

    var username = $('#username').val();
    var password = $('#password').val();

     $.ajax({
      url:'check.php',
      method:"POST",
      data:{user_name:username, password:password},
      success:function(data)
      {
        console.log(data);
       if(data != '0')
       {
        $('#availability').html('<span class="text-danger">Username and Password not Match</span>');
        $('#register').attr("disabled", true);
       }
       else
       {
        $('#availability').html('<span class="text-success">Username and Password Available</span>');
        $('#register').attr("disabled", false);
       }

      }

     })

  });
 });  
</script>

check.php - database connection and query

  <?php  
    //check.php  
    $connect = mysqli_connect("localhost", "username", "", "dbname"); 
    if(isset($_POST["user_name"], $_POST["password"]))
    {
     $username = mysqli_real_escape_string($connect, $_POST["user_name"]);
     $password = mysqli_real_escape_string($connect, $_POST["password"]);
     $query = "SELECT * FROM admin WHERE username = '".$username."' AND password = '".$password."' ";
     $result = mysqli_query($connect, $query);
     echo mysqli_num_rows($result);
    }
 ?>
</div>
  • 写回答

3条回答 默认 最新

  • douqiao4450 2019-05-15 05:59
    关注

    I think your condition is wrong in the client end script. If username and password matched then it will return value which is greater than 0 . In that case, your code should be

    success:function(data)
      {
        console.log(data);
        if(data == 0)
        {
          $('#availability').html('<span class="text-danger">Username and Password not Match</span>');
          $('#register').attr("disabled", true);
        } 
        else
        {
          $('#availability').html('<span class="text-success">Username and Password Available</span>');
          $('#register').attr("disabled", false);
        }
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。