Yudi Moszkowski
Yudi Moszkowski

Reputation: 80

Return php error from Ajax call

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 :)

Upvotes: 0

Views: 266

Answers (1)

Hossein
Hossein

Reputation: 1391

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.

Upvotes: 1

Related Questions