来自Ajax和php的错误响应

时间:2018-07-10 13:45:27

标签: php html json ajax

你好,我不知道我在做什么错。我正在尝试将ajax用于登录页面。 ajax正在运行,但是成功的响应不是来自我指定的文件。我需要它来读取login_validate.php的响应,但是我正在从dbconnect.php获取响应,而该响应不应该那样工作。

这是 index.php ,这是ajax页面

jQuery(document).ready(function(){
 jQuery(".login").submit(function(e){
    e.preventDefault();
    var formData = jQuery(this).serialize();
    $.ajax({
        type:"POST",
        url:"login_validate.php",
        data:formData,
        success: function(response){
            /** alert (response);
         }, error: function(jqXHR, textStatus, errorThrown){
                alert('error');
         } **/       
            if(response == "True")
            {
                //alert('Yes');
                //$.jGrowl("Loading File Please Wait......", { sticky: true });
                $.jGrowl("Welcome... Redirecting", { header: 'Login Successful' });
                var delay = 3000;
                setTimeout((function(){ window.location = 'welcome.php'  }), delay);

            }else{
                //alert('No');
                $.jGrowl("Invalid Login Details", { header: 'Access Denied' });
            }
        }
    });
    return false;
});

});                         

这是 dbconnect.php

<?php
// connect to database
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "cbt2";


 $conn = new mysqli($hostname, $username, $password, $dbname);
 // check connection
 if ($conn->connect_error) {
    die ('Error connecting to database' . $conn->connect_error);
 } else {
    echo "Connection Successful";
 }
 ?> 

这是 login_validate.php

<?php
require 'admin/dbconnect.php';
require 'admin/core.php';

if(loggedin())
{
    header('location:welcome.php');
}
if (isset($_POST['loginbtn'])){
    $name=$_POST['username'];
    $pass= $_POST['password'];

    $qry="SELECT * FROM student WHERE username='$name' AND password='$pass' LIMIT 1";
    $qrycheck=$conn->query($qry);
    if ($qrycheck->num_rows > 0){
        while($fetch = $qrycheck->fetch_assoc()){
            $class=$fetch['class'];
            $fullname=$fetch['fullname'];
            $username=$fetch['username'];
            $dept=$fetch['dept'];
            $id=$fetch['id'];

            $_SESSION['stdid']=$id;
            $_SESSION['user']=$username;
            $_SESSION['name']=$fullname;
            $_SESSION['class']=$class;
            $_SESSION['dept']=$dept;

        }
        echo "True";
    } else {
        echo "False";
    }
}
?>

所以我得到的成功回复是echo 'Connection Successful',应该是echo 'True'

2 个答案:

答案 0 :(得分:2)

dbconnect.php

中删除else语句
<?php
// connect to database
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "cbt2";




$conn = new mysqli($hostname, $username, $password, $dbname);
 // check connection
 if ($conn->connect_error) {
    die ('Error connecting to database' . $conn->connect_error);
 } 
 ?>

连接成功后,您的dbconnect.php将回显“连接成功”,这是ajax首先读取的,这不是您在等待什么。甚至不建议在成功连接到数据库后每次都回显或记录日志。

更新:

还更新您的 login_validate.php

<?php
require 'admin/dbconnect.php';
require 'admin/core.php';


if (isset($_POST['username'], $_POST['password'])){
    $name=$_POST['username'];
    $pass= $_POST['password'];

    $qry="SELECT * FROM student WHERE username='$name' AND password='$pass' LIMIT 1";
    $qrycheck=$conn->query($qry);
    if ($qrycheck->num_rows > 0){
        while($fetch = $qrycheck->fetch_assoc()){
            $class=$fetch['class'];
            $fullname=$fetch['fullname'];
            $username=$fetch['username'];
            $dept=$fetch['dept'];
            $id=$fetch['id'];

            $_SESSION['stdid']=$id;
            $_SESSION['user']=$username;
            $_SESSION['name']=$fullname;
            $_SESSION['class']=$class;
            $_SESSION['dept']=$dept;

        }
        echo "True";
    } else {
        echo "False";
    }
}
?>

当您从ajax调用 php 文件时,您无需在某处重定向,它应该只返回一些内容。另外,我认为检查$_POST['loginbtn']是否设置是多余的。

答案 1 :(得分:1)

对AJAX调用的答复将是PHP脚本输出的所有内容

因此您需要从此处删除echo "Connection Successful";

<?php
// connect to database
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "cbt2";


 $conn = new mysqli($hostname, $username, $password, $dbname);
 // check connection
 if ($conn->connect_error) {
    die ('Error connecting to database' . $conn->connect_error);
 //} else {
    //echo "Connection Successful";
 }
 ?>