登录验证无法使用JSON和PHP的HTML工作
问题描述:
我使用JSON和PHP在HTML上创建登录表单,但成功的所有if
语句不起作用,但beforeSend
和error
正在工作。你能帮我检查我的错误吗?登录验证无法使用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);
?>
答
你有
echo json_encode($response);
echo json_encode($_POST);
这是会发布损坏的JSON。例如你的输出将是
{"success":true,"message":"Login successful"}Array
^^^^^^---corruption
由于您的JSON被破坏,就不能正确解码,并response
不会是什么你认为它是。
+0
谢谢老板。你的帮助是有意义的。你修好了 – user3131505
答
删除此行:
echo json_encode($_POST);
你尝试过什么检查响应的实际价值是什么?当你做'console.log(response)'时出现了什么? – Osuwariboy
您确实应该使用PHP的[内置函数](http://jayblanchard.net/proper_password_hashing_with_PHP.html)来处理密码安全性。 –