ajax登录错误,无法检查

时间:2016-03-14 03:18:33

标签: php jquery ajax

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

的login.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>

model.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();
    }
}
?>

ajax.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;
    });
});

2 个答案:

答案 0 :(得分:1)

您应该传递这样的数据

$(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']);

答案 1 :(得分:0)

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

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