Ajax 登录错误,无法检查


ajax login error, can't check

我有问题,我无法检查usernamepassword,你能帮我吗?

登录.php:

<form class="login-form" method="post" name="loginform" action="">
    <div class="title-section">
        <h1><span>Login</span></h1>
    </div>
    <p>Welcome! Login in to your account</p>
    <label for="user_login">Username or email address<span>*</span>
    </label>
    <input type="text" name="log" id="user_login">
    <label for="user_pass">Password<span>*</span>
    </label>
    <input name="pwd" id="user_pass" type="password">
    <div id="error"></div>
    <button type="submit" name="submit-login" id="submit-login"> <i class="fa fa-arrow-circle-right"></i> Login </button>
</form>

型号.php;

<?php 
ob_start();
session_start(); 
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
?>
<?php
global $pdo;
session_start();
if(isset($_POST['submit-login'])) {
    $user_email = trim($_POST['log']);
    $user_password = trim($_POST['pwd']);
    try { 
        global $pdo;
        $stmt = $pdo->prepare("SELECT * FROM user WHERE username=:email");
        $stmt->execute(array(":email"=>$user_email));
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        $count = $stmt->rowCount();
        if($row['user_password']==$password){
            echo "ok"; // log in
            echo $_SESSION['user_session'] = $row['id_user'];
        }
        else {
            echo "email or password does not exist."; // wrong details 
        }
    }
    catch(PDOException $e){
        echo $e->getMessage();
    }
}
?>

阿贾克斯.js

$(document).ready(function(){
    $("#submit-login").click(function(){
        username=$("#user_login").val();
        password=$("#user_pass").val();
        $.ajax({
            type: "POST",
            url: "admin/module/admin/model/acc_model.php",
            data: "name="+username+"&pwd="+password,
            success: function(html){    
                if(html=='ok') {
                    window.location="profile.php";
                }
                else {
                    alert('Please, Error');
                }
            },
            beforeSend:function() {
                $("#error").html("<img src='http://www.chinesecio.com/templates/base/images/loading.gif' /> Loading...")
            }
        });
        return false;
    });
});

你应该像这样传递数据

$(document).ready(function(){
  $("#submit-login").click(function(){
    username=$("#user_login").val();
    password=$("#user_pass").val();
    $.ajax({
            type: "POST",
            url: "admin/module/admin/model/acc_model.php",
            data: {
                name : username,
                pwd  : password
            },
            success: function(html){    
                if(html=='ok')    {
                    window.location="profile.php";
                }
                else    {
                  alert('Please, Error');
                }
            },
            beforeSend:function()
            {
                $("#error").html("<img src='http://www.chinesecio.com/templates/base/images/loading.gif' /> Loading...")
            }
          });
          return false;
  });
});

还要使用如下所示的参数:

$user_email = trim($_POST['name']);
$user_password = trim($_POST['pwd']);

我认为当您从 ajax 请求发送数据时,您使用的是不同的变量名称,并且在您的模型中.php您使用的是表单元素名称。请检查一下

data : {"name":username, "password ":password}