duan1930 2017-01-16 15:00
浏览 26
已采纳

使用ajax和php登录总是错误的密码

I tried this code to use for login but it does not work. when I click on submit, nothing happens. When I tried to login I am always getting a wrong password error, but my password is correct. what should I do?

my HTML

<form class="form-horizontal" id="loginform"   method="post">
                    <div class="form-group">
                        <label for="userid" class="col-sm-3 control-label">User ID</label>
                        <div class="col-sm-9 has-feedback">
                            <input type="text" class="form-control" id="userid" name="userid" placeholder="Enter Your User ID" required>
                            <span class="glyphicon glyphicon-user form-control-feedback"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="password" class="col-sm-3 control-label">Password</label>
                        <div class="col-sm-9 has-feedback">
                            <input type="password" class="form-control" id="password" name="password" placeholder="Enter Your Password" required>
                            <span class="glyphicon glyphicon-lock form-control-feedback"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-xs-4 col-xs-offset-8">
                            <button type="submit" id="submit" class="btn btn-danger btn-block btn-flat">Sign In</button>
                        </div>
                    </div>                  
                </form>

my Php

 <?php
        $userid = security($_POST['userid']);
                $password = security($_POST['password']);

                $query = mysqli_query($link,"SELECT * FROM user WHERE USER_USERNAME = '".$userid."' AND USER_PASSWORD = '".$password."'");
                $numrows = mysqli_num_rows($query);
                if ($numrows!==0) 
                {
                    while($row = mysqli_fetch_assoc($query))
                    {
                        @$_SESSION['session_userid'] = $row['USER_USERNAME'];
                        @$_SESSION['session_accounttype'] = $row['ACCESS_ID'];
                    }
                    //Location Depends on the User Type
                    if($_SESSION['session_accounttype']=="1")
                    {
                            echo 'true';
                    }
                    }
              else
                {
                    echo 'false';
                }
        ?>

My JS

$(document).ready(function(){
    $("#add_err").css('display', 'none', 'important');
     $("#submit").click(function(){
          username=$("#userid").val();
          password=$("#password").val();
          $.ajax({
           type: "POST",
           url: "../basefunction/login.php",
            data: "userid="+username+"&password="+password,
           success: function(html){    
            if(html=='true')    {
             //$("#add_err").html("right username or password");
             window.location="admin";
            }
            else    {
            $("#add_err").css('display', 'inline', 'important');
             $("#add_err").html("<img src='images/alert.png' />Wrong username or password");
            }
           },
           beforeSend:function()
           {
            $("#add_err").css('display', 'inline', 'important');
            $("#add_err").html("<img src='images/ajax-loader.gif' /> Loading...")
           }
          });
        return false;
    });
});
  • 写回答

2条回答 默认 最新

  • doubu0897 2017-01-16 16:39
    关注

    Why other answers talk the salt for password?question asks why password was wrong. I think this code have questions if($_SESSION['session_accounttype']=="1")

    The session_accounttype may be not eq 1?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多