簡體   English   中英

從Ajax調用返回php錯誤

[英]Return php error from Ajax call

我有一個php腳本,我想用ajax調用它,我從ajax遇到了一個錯誤,錯誤消息為“ Error: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>

謝謝你的時間 :)

似乎您輸入了錯誤的url:在html動作中,您使用“ php / login.php”,而在ajax調用中,您在其之前使用與“ ../”相同的url。 如果您解釋login.php和此html文件的位置,將有助於解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM