2016-05-16 4 views
0

私はPHPスクリプトを持っていて、それをajaxで呼びたいと思っています。 "Error:email = my_email & password = myPassword"というajaxからエラーが出ました。ここで 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

データに何が入っているのかというエラーがあるようです。あなたのURLは正しいですか? –

+0

@BubbleHackerどのURLですか? –

+0

AJAXリクエスト(jQueryコード) –

答えて

1

あなたは間違ったURLを入力したようです: htmlアクションでは、 "php/login.php"を使用し、ajax呼び出しでは、 "../"と同じURLを使用します。 login.phpとこのhtmlファイルの場所を説明すると、問題を解決するのに役立ちます。

+0

StackOverflowで話すテキストを使用しないでください。 「あなた」のような言葉を綴ってください。それ以外の場合は、良い答えです。 – gibberish

関連する問題