【发布时间】:2017-02-03 08:55:40
【问题描述】:
我正在编写 PHP 代码来验证 mysql 数据库中是否存在用户名和密码。当我单击登录页面 (index.html) 上的登录按钮时,将调用此 php。我的php代码如下:
<?php
require_once 'dbconfig.php';
// Unescape the string values in the JSON array
$logindata = stripcslashes($_POST['pLogData']);
// Decode the JSON array
$logindata = json_decode($logindata,TRUE);
// now $tableData can be accessed like a PHP array
$user_email = $logindata['user'];
$user_password = $logindata['pass'];
//First lets get the username and password from the user
$query = $db_conn->prepare("SELECT * FROM tblUsers WHERE email=:user_email LIMIT 1");
$query->execute(array(':user_email'=>$user_email));
$userRow=$query->fetch(PDO::FETCH_ASSOC);
if($query->rowCount() > 0) {
if(password_verify($upass, $userRow['pass'])) {
$_SESSION['user_session'] = $userRow['userid'];
if($userRow['userid'] == 1) {
header("Location: adminPanel.html");
} else {
header("Location: main_dashboard.html");
}
// return true;
} else {
header("Location: index.html");
// return false;
}
}
?>
ajax调用代码为:
function login(username, passwd) {
var loginData = {
'user': username,
'pass': passwd
};
loginData = $.toJSON(loginData);
$.ajax({
type: "POST",
url: "loginValidate.php",
data: "pLogData=" + loginData,
success: function(msg) {
console.log(msg);
// return value stored in msg variable
}
});
}
除了在 php 中设置的导航没有发生外,它工作正常。我无法弄清楚错误在哪里。它必须导航到的整个 html 正在打印到控制台中。
任何帮助将不胜感激。
【问题讨论】:
-
通过 ajax 是行不通的。返回
file name并在javascript 中使用window.location=filename。或提交表单,如果 uncussessfull 重定向到main_dashboard.html上的当前 else。 -
@Suchit,您的建议可行,但我需要访问服务器以验证用户名和密码。使用php很容易。如果我用 php 文件替换 html,它可以切换。我想知道为什么它没有切换到 html 文件。
-
.html 无法识别 php 代码。如果您想使用 ajax,则在验证后返回文件名并使用它来重定向。
-
好的。会试试的
-
您可以使用 ajax 加载整个
main_dashboard.html,然后使用innerHTML将您的html内容写入您的index页面的正文。
标签: javascript php jquery html mysql