从PHP登录请求获取空的AJAX响应
16lz
2021-01-22
I have the following php code, which works on its own. However, when using it AJAX it returns an empty response.
我有下面的php代码,它自己工作。然而,在使用AJAX时,它会返回一个空响应。
PHP login code:
PHP登录代码:
<?php
session_start();
include_once 'resources/database.php';
if(isset($_POST['m_login_signin_submit'])) {
$email = trim($_POST['email']);
$user_password = trim($_POST['password']);
$password = MD5($user_password);
try {
$stmt = $db->prepare("SELECT * FROM users WHERE email=:email");
$stmt->execute(array(':email' => $email));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();
if($row['password']==$password) {
echo '1';
$_SESSION['user_session'] = $row['id'];
} else {
echo 'You do not matter.';
}
} catch (PDOException $ex) {
echo $ex->getMessage();
}
}
AJAX Code:
AJAX代码:
$(document).ready(function () {
/* validation */
$("#login-form").validate({
submitHandler: submitForm
});
function submitForm() {
var email = $('#email').val();
var password = $('#password').val();
console.log(data);
$.ajax({
type: 'POST',
dataType: 'text',
url: 'partials/processLogin.php',
data: {
email:email,
password:password
},
success: function (response) {
console.log("Checking success.");
console.log(response);
},
error: function () {
console.log("error");
}
});
return false;
}
});
});
In console, all the values pass correctly and it returns success but the value is empty. Any help is greatly appreciated.
在控制台,所有的值都正确地传递,它返回成功,但是值是空的。非常感谢您的帮助。
Thank you
谢谢你!
1 个解决方案
#1
3
You did not pass any m_login_signin_submit
in your request, therefore you never go into your main if statement. I'm guessing that is the form's submit button but since this is ajax it will not get sent unless you do it explicitly.
在您的请求中,您没有传递任何m_login_signin_submit,因此您永远不会进入您的主if语句。我猜这是表单的提交按钮,但由于这是ajax,除非您显式地执行,否则它不会被发送。
data: {
email:email,
password:password,
m_login_signin_submit: 1
},
更多相关文章
- php mail函数一段好的代码
- 用于上传多个文件的PHP代码
- (phpQuery)对网站产品信息采集代码的优化
- 韩顺平_php从入门到精通_视频教程_学习笔记_源代码图解_PPT文档
- 【MySQL 技巧分享】 mysql -e 加 v 简化代码
- Oracle相当于MySQL代码“插入虚拟”以返回错误消息
- 登录使用PHP并´t显示任何html代码
- 在代码点火器中从mysql迁移到postgresql
- 【动软.Net代码生成器】连接MySQL生成C#的POCO实体类(Model)