从Ajax调用返回php错误

时间:2016-05-16 18:28:57

标签: javascript php jquery ajax

我有一个PHP脚本,我希望用ajax调用它,我有一个错误来自ajax,它说"错误:email = my_email& password = myPassword"。

这是PHP脚本

<?php

session_start(); //starts a session in order to be-able to save session variables and to read them

require "db_config.php"; //Allows us to use the database connection from db_config.php in this file

if ($_SERVER["request_method"] == "post"){ //checks if the form was submitted

    $email = $_POST["email"]; //fetching the email address which was inserted in the login.html form
    $password = $_POST["password"]; //fetching the password which was inserted in the login.html form

    /*

    querying the database, to check whether there is a result with the email and password entered by the user

    */

    $checkForUser = mysqli_query($db_connection, "SELECT * FROM `tbl_users` WHERE email = '$email' and Password = '$password' LIMIT 1");

    /*

    checking if the query resulted in one row, if there is a row
    it means there is a user with this email and password, which means these are the correct creadentials

    */

    $rows = mysqli_num_rows($db_connection, $checkForUser);

    if ($rows == 1){ 

        //this means: correct credentials

        //the next few lines fetch the information from the result
        while($row = mysqli_fetch_assoc($checkForUser)){
            $_SESSION["user_id"] = $row["userId"]; //creates a session variable containing the users id
            $_SESSION["users_name"] = $row["firstName"]. " ".$row["lastName"]; //creates a session variable containing the users name
        }

        echo "You are now logged in: ". $_SESSION["users_name"];

    }else{

        //this means: incorrect credentials
        echo "Incorrect Username or password"; //prints out error message

    }

}

?>

这是main.js

$(document).ready(function() {

    $('#loginForm').submit(function() {
        var data = $(this).serialize();

        $.ajax({
        url: "../php/login.php",
        type: "POST",
        data: data,
            success: function(data) {
                $('*').html(data);
            },
            error: function() {
                alert('ERROR: ' + data);
            }
        });

        return false;

    });

});

以下是可能有帮助的login.html页面

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="css/main.css">
    <link rel="stylesheet" type="text/css" href="css/responsive.css">
    <script src="js/jquery.js"></script> 
    <script src="js/main.js"></script>
    <meta name="copyright" content="Yudi Moszkowski">
    <title>Login | Your Site Name</title>
</head>
<body>
    <div id="loginContainer">
        <div class="logo"><img src="img/yourLogo.png"></div>
        <form action="php/login.php" method="post" id="loginForm">
            <input type="text" name="email" placeholder="Email" id="loginEmail" class="loginInput" required="true">
            <input type="password" name="password" placeholder="Password" id="loginPassword" class="loginInput" required="true"/>
            <input type="submit" value="Login" name="loginSubmit" id="loginSubmit">
        </form>
        <div id="loginOptions"><p id="noAccount">Not signed up? <a href="signup.html">Signup</a></p><p id="forgotPass"><a href="forgot_pass.html">Forgot password?</a></p></div>
    </div>
</body>
</html>

感谢您的时间:)

1 个答案:

答案 0 :(得分:1)

您似乎输入了错误的网址: 在html动作中,你使用&#34; php / login.php&#34;在ajax调用中,你使用相同的URL和#34; ../"在它之前。如果你解释一下login.php和这个html文件的位置,那么解决你的问题会很有帮助。