2016-05-16 122 views
0

我有一個PHP腳本,我希望它與Ajax調用,我有一個錯誤來自ajax說:「錯誤:電子郵件= my_email &密碼= myPassword」。從Ajax調用返回php錯誤

這裏是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; 

    }); 

}); 

這裏是一個可能會有所幫助

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

感謝您的時間的login.html頁:)

+0

這似乎是錯誤,因爲這是數據放入。你的網址是否正確? –

+0

@BubbleHacker哪個網址? –

+0

在您的AJAX請求(jQuery代碼) –

回答

1

它似乎是你輸入錯誤的網址: 在html動作中,你使用「php/login.php」和ajax調用,你使用與「../」相同的URL前面。如果你解釋login.php和這個html文件的位置,這將有助於解決你的問題。

+0

請不要在StackOverflow上使用發短信。請拼出像'你'等字,否則,一個很好的答案 - 做得很好。 – gibberish