2015-07-09 66 views
0

我使用JSON和PHP在HTML上創建登錄表單,但成功的所有if語句不起作用,但beforeSenderror正在工作。你能幫我檢查我的錯誤嗎?登錄驗證無法使用JSON和PHP的HTML工作

我不知道成功的功能有什麼問題。警報不會彈出。例如response.success == true應該彈出 '您成功登錄......'

<script> 
$(document).ready(function(){ 
    $('#loginForm').on('submit',function(e){ 

     var myForm = new FormData($(this)[0]); 

     $.ajax({ 
      type:'POST', 
      url: 'connections/login.php', 
      data : new FormData($(this)[0]), 
      cache: false, 
      contentType:false, 
      processData: false, 
      beforeSend: function(){ 
       $("div#divLoading").show(); 
      }, 
      success: function(response){ 
       $("div#divLoading").hide(); 
       console.log(response); 
       if(response.success == true) 
       { 
        alert(' You are successfully logged in... ') 
       } 

       else if(response.success == false){ 
        alert('please enter a correct email & password'); 
       } 
       else{ 
        if(response.matric){ 
         alert('email is wrong'); 
        } 
        if(response.password){ 
         alert('password is wrong'); 
        } 
       } 
      }, 
      error: function(data){ 
       alert('error'); 
       $("div#divLoading").hide(); 
      } 
     }); 
    return false; 
    }); 
}); 
</script> 

這是我的PHP:

<?php 
require_once('connect.php'); 

session_start(); 

header('Content-Type: application/json'); 
if (!empty($_POST['matric'])) 
{ 
$matric=$_POST['matric']; 
$password=$_POST['password']; 



$pass= $dbh->prepare("SELECT * FROM users WHERE matric=:matric AND password=:password"); 
$pass->bindParam(':matric', $matric); 
$pass->bindParam(':password', $password); 
$pass->execute(); 
if($pass->fetch(PDO::FETCH_NUM) > 0) 
{ 
    $_SESSION['matric']=$matric; 
    $response = array(
    'success' => true, 
    'message' => 'Login successful'); 
} 
else 
{ 
    $response = array(
    'success' => false, 
    'message' => 'Login fail'); 
} 

} 

echo json_encode($response); 
echo json_encode($_POST); 

?> 
+0

你嘗試過什麼檢查響應的實際價值是什麼?當你做'console.log(response)'時出現了什麼? – Osuwariboy

+0

您確實應該使用PHP的[內置函數](http://jayblanchard.net/proper_password_hashing_with_PHP.html)來處理密碼安全性。 –

回答

0

你有

echo json_encode($response); 
echo json_encode($_POST); 

這是會發布損壞的JSON。例如你的輸出將是

{"success":true,"message":"Login successful"}Array 
              ^^^^^^---corruption 

由於您的JSON被破壞,就不能正確解碼,並response不會是什麼你認爲它是。

+0

謝謝老闆。你的幫助是有意義的。你修好了 – user3131505

0

刪除此行:

echo json_encode($_POST);