2016-12-23 13 views
1

私はAjaxを学び、PHPでログインページを作成しようとしています。なぜ私のコードが動作していないのか分かりません。ログインボタンをクリックするたびに、私のページは私のログイン機能をチェックせずに別のページ(例えばexam.php)にリダイレクトされます。私のコードは以下の通りです。誰かが私に問題を見つけるのを手伝ってくれたら嬉しいです。ログインページでAjaxが機能しない

HTML

<form action="" method="post"> 
<input name="email" type="text" id="email"> 
<input name="password" type="password" id="password"> 
<input type="submit" name="login" id="loginsubmit" value="Login"> 
</form> 
    <span class="empty" style="display:none">Field must be empty..</span> 
    <span class="error" style="display:none">Email or Password not matched !!!</span> 
    <span class="disable" style="display:none">User Id Disabled.</span> 

AJAXコード

$(function(){ 
// For user login 
$("#loginsubmit").click(function(){ 
    var email  = $("#email").val(); 
    var password = $("#password").val(); 
    var dataString = 'email='+email+'&password='+password; 
    $.ajax({ 
     type:"POST", 
     url:"getlogin.php", 
     data:dataString, 
     success: function(data){ 
      if ($.trim(data) == "empty") { 
       $(".empty").show(); 
      }else if($.trim(data) == "disable") { 
       $(".disable").show(); 
      }else if($.trim(data) == "error"){ 
       $(".error").show(); 
      } else { 
       window.location = "exam.php"; 
       //document.write = "You shall not pass"; 
      } 
     } 
    }); 
    return false; 
}); 
}); 

getlogin.php

$filepath = realpath(dirname(__FILE__)); 
include_once ($filepath.'/classes/user.php'); 
$usr = new User(); 

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    // this values are coming from main.js 
    $email = $_POST['email']; 
    $password = md5($_POST['password']);   
    $userlogin = $usr->userLogin($email, $password); // function created in user.php page 
} 

user.php

$filepath = realpath(dirname(__FILE__)); 
include_once ($filepath.'/../lib/Session.php'); 
include_once ($filepath.'/../lib/Database.php'); 
include_once ($filepath.'/../helpers/Format.php'); 

public function userLogin($email, $password){ 
    $email  = $this->fm->validation($email); 
    $password = $this->fm->validation($password); 
    $email  = mysqli_real_escape_string($this->db->link, $email); 
    $password = mysqli_real_escape_string($this->db->link, $password); 

     if ($email == "" || $password == "") 
     { 
      echo " empty"; 
      exit(); 
     } else { 
      $query = "SELECT * FROM tbl_user WHERE email = '$email' AND password = '$password'"; 
      $result = $this->db->select($query); 
      if ($result != false) { 
       $value = $result->fetch_assoc(); 
       if ($value['status'] == '1') { 
        echo " disable"; 
        exit(); 
       } else { 
        Session::init(); 
        Session::set("login", true); 
        Session::set("userid", $value['userid']); 
        Session::set("username", $value['username']); 
        Session::set("name", $value['name']); 
       } 
      } else { 
       echo " error"; 
       exit(); 
      } 
     } 
    } 
+0

https://api.jquery.com/event.preventdefault/をご覧ください。 [MCVE] – miken32

+0

も参照してください。Userクラスの定義はどこですか? –

+0

$ usr = new User();それをgetlogin.phpページに追加しました。 –

答えて

1

は、私は問題は、不在Userクラス定義であるので、このスクリプトの出力は致命的なエラーであり、それはAJAX呼び出し中にあなたelseステートメントに直接行くと信じています。

クロムでは、[ネットワーク]タブのデベロッパーツールを使用して、ajaxリクエストの出力を確認できます。

+0

$ usr = new User(); getlogin.phpページに追加しました –

+0

クラス自体の定義をuser.php内にしてはいけませんか?クラスをどこにも定義せずに 'User'クラスの' userLogin'メソッドを呼び出しています。 –

+0

クラスユーザ \t { \t \t private $ db; \t \tプライベート$ fm; \t \tパブリック関数__construct() \t \t { \t \t \tます$ this->デシベル=新しいデータベース(); \t \t \t $ this-> fm = new Format(); \t \t –

0

ボタンの種類を[送信]から[ボタン]に変更します。

関連する問題