Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Ajax登录问题与响应_Php_Jquery_Ajax - Fatal编程技术网

Php Ajax登录问题与响应

Php Ajax登录问题与响应,php,jquery,ajax,Php,Jquery,Ajax,通过Ajax登录不起作用,并且显示登录失败。 下面是索引文件的代码片段 $('.signin-button-department').click(function(e) { e.preventDefault(); var short = $.trim($('input[name="short_name"]').val()); var passs = $.trim($('input[name="inst_password"

通过Ajax登录不起作用,并且显示登录失败。 下面是索引文件的代码片段

 $('.signin-button-department').click(function(e) {
        e.preventDefault();

    var short = $.trim($('input[name="short_name"]').val());
    var passs = $.trim($('input[name="inst_password"]').val());

    $.ajax({
            type: 'POST',
            url: 'login.php',
            data: {
                short_name: short, 
                 inst_password: passs 
            }

        })
        .done(function(res) {
            console.log(res);
            if (res == 1) {
                window.location.href ="production/institute.php";
            } else {
                alert('Login failed!');
                $('input[name="short_name"]').focus();
                $('input').val('');
            }
        });
}); //Department
这是
login.php
代码

<?php
    include('connect.php');
    
    $inst_id = $_POST['short_name'];
    $inst_password = md5($_POST['inst_password']);
    
    $sql = "SELECT * FROM institute WHERE short_name = ?  AND inst_password = ? ";
    $query = $conn->prepare($sql);
    
    $query->execute(array($inst_id,$inst_password));
    $row = $query->fetch();
    $count = $query->rowCount();
    
    if ($count > 0){
        session_start();
        $_SESSION['id'] = $row['institute_id'];
        echo 1;
    }else{
        echo 0;
    }
?>


您能看一下并告诉我哪里出了问题吗?

不要使用密码哈希。它甚至在手册中这样说:“由于此哈希算法的快速性,不建议使用此函数保护密码。”用于哈希密码,并在登录时根据密码验证哈希。这将要求您重写代码,因此我建议您在继续调试之前改为这样做。ajax回调返回的响应将是一个字符串,而不是您在这里测试的整数。强制转换为int或将其视为字符串。@ProfessorAbronsius-这是JavaScript,他们使用松散的类型比较,所以这两种方式都可以。@farhadKhan您在“网络”选项卡中检查过ajax响应了吗?你能和我们分享一下吗?你的代码应该可以工作,这可能是数据的问题。在将MD5应用于密码后,使用var_dump()检查您在PHP中收到的值,并直接在数据库中使用这些值运行查询,以查看它是否返回预期结果