登录验证不能在使用JSON和PHP的HTML上工作
Login validation not working on HTML using JSON and PHP
我正在使用JSON和PHP在HTML上制作登录表单,但所有关于成功的if
语句都不起作用,但beforeSend
和error
正在工作。你能帮我检查一下错误吗?
我不知道成功的函数有什么问题。警报不会弹出。例如,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
不会是你认为的。
删除这一行:
echo json_encode($_POST);
相关文章:
- 通过点击-PHP/HTML发布列表中的选定值
- 在网格中显示来自 PHP [HTML 的数据库的数据]
- 我希望我的php/html网站上的JavaScript能够实时更新
- 我在firebug控制台中得到了index.php.html
- 如何从php/html检查网站是否已打开
- jQuery Slider PHP/HTML Problems
- PHP/HTML:下载PDF链接处理页面加载
- 如何在 php/html 表单中编写链接
- 从生成的 PHP-HTML 表中获取数据库值
- PHP/HTML:如果 url domain.co.uk/?debug 那么
- 免费的应用程序来混淆和缩小JavaScript / PHP / HTML for Mac
- PHP/html:即时搜索功能:无法搜索外语(例如中文)字符
- Php/HTML/css:链接不可点击
- 在 PHP HTML 中向后单击时显示除图像外的所有值
- POST with ajax, php, html
- jquery json PHP HTML 不显示结果
- PHP HTML 表单选择选项来填充文本字段 jQuery
- PHP & HTML Form with javascript onsubmit not executed
- PHP/HTML:将HTML变量保存在PHP会话变量中
- 如何将渲染的PHP+HTML代码转换为文本文件