dongsigan2044 2016-05-16 18:28
浏览 60
已采纳

从Ajax调用返回php错误

I have a php script and I would like it to be called with ajax, I have had an error coming from the ajax which says "Error:email=my_email&password=myPassword".

Here is the PHP script

<?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

    }

}

?>

Here is 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;

    });

});

Here is the login.html page which may be helpful

<!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>

Thanks for your time :)

  • 写回答

1条回答 默认 最新

  • doucongqian6644 2016-05-16 18:53
    关注

    it seem that you type wrong url : in html action, u use "php/login.php" and in ajax call, u use same url with "../" before it. if you explain the location of login.php and this html file, it will be helpful to solve your problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器