登录验证不能在使用JSON和PHP的HTML上工作

Login validation not working on HTML using JSON and PHP

本文关键字:PHP HTML 工作 JSON 不能 验证 登录      更新时间:2023-09-26

我正在使用JSON和PHP在HTML上制作登录表单,但所有关于成功的if语句都不起作用,但beforeSenderror正在工作。你能帮我检查一下错误吗?

我不知道成功的函数有什么问题。警报不会弹出。例如,response.success == true应该弹出"您已成功登录……"'

<script>
$(document).ready(function(){
    $('#loginForm').on('submit',function(e){
        var myForm = new FormData($(this)[0]);
        $.ajax({
            type:'POST',
            url: 'connections/login.php',
            data : new FormData($(this)[0]),
            cache: false,
            contentType:false,
            processData: false,
            beforeSend: function(){
                $("div#divLoading").show();
            },
            success: function(response){
                $("div#divLoading").hide();
                console.log(response);
                if(response.success == true)
                {
                    alert(' You are successfully logged in... ')
                }
                else if( response.success == false ){
                    alert('please enter a correct email & password');
                }
                else{
                    if(response.matric){
                        alert('email is wrong');
                    }
                    if(response.password){
                        alert('password is wrong');
                    }
                }
            },
            error: function(data){
                alert('error');
                $("div#divLoading").hide();
            }
        });
    return false;
    });
});
</script>

这是我的PHP:

<?php
require_once('connect.php');
session_start();
header('Content-Type: application/json');
if (!empty($_POST['matric']))
{
$matric=$_POST['matric'];
$password=$_POST['password'];

$pass= $dbh->prepare("SELECT * FROM users WHERE matric=:matric AND password=:password");
$pass->bindParam(':matric', $matric);
$pass->bindParam(':password', $password);
$pass->execute();
if($pass->fetch(PDO::FETCH_NUM) > 0)
{
    $_SESSION['matric']=$matric;
    $response = array(
    'success' => true,
    'message' => 'Login successful');
}
else
{
    $response = array(
    'success' => false,
    'message' => 'Login fail');
}
}
echo json_encode($response);
echo json_encode($_POST);
?>

你有

echo json_encode($response);
echo json_encode($_POST);

将发出损坏的JSON。例如,你的输出将是

{"success":true,"message":"Login successful"}Array
                                             ^^^^^^---corruption

因为你的JSON是损坏的,它不会正确解码,response不会是你认为的。

删除这一行:

echo json_encode($_POST);