我试图在不离开页面的情况下针对我的数据库验证用户信息,并且只有在信息正确的情况下,它才应该离开页面并将用户路由到用户门户。 PHP可以正常工作,可以检查信息,如果正确,则将用户路由到门户,如果为false,则将路由到空白页。
我要做的是拦截提交,对数据库进行验证,如果为false,则输出错误消息,如果正确,则路由用户。
我认为我面临的错误是如何使PHP以某种方式“发出信号”到jQuery,这是错误的
我创建了一个提交处理程序,该处理程序停止提交表单,然后将其序列化并将其发送到checkLogin.php,但此后没有任何结果。
如果信息正确,我没有将用户发送到任何地方,因此,如果用户名和密码正确,则不会发生任何事情
这是checkLogin.php -用实际连接的数据库替换了DB,因为我不在本地主机上托管,所以信息是私有的
<?php
$link = mysqli_connect('DB', 'DB', 'DB', 'DB'); //Database Connection
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$username = mysqli_real_escape_string($link, $_POST['username']); //Retrieving the username
$password = mysqli_real_escape_string($link, $_POST['password']); //Retrieving the password
$user_sql = "SELECT username FROM login WHERE username = '$username'"; //Setting the SQL query
$user_check = mysqli_query($link, $user_sql); //Putting the result of the query (^) into a variable
if ($user_check) {
$test = "THIS WAS A SUCCESS";
} else {
$test = "THIS WAS A FAILURE";
}
?>
这是login.js中的函数-用户单击提交后调用该函数
function checkLogin() {
$("#login_form").submit(function(event) { //Submit Handler
var post = $.post("checkLogin.php", $form.serialize());
post.done(function() { //When the function is successfully submitted
});
//$("#login_form").submit(); //Submit the form (blocked for now)
});
}
login.html-表单
<form action="login.php" method="post" name="login_form" id="login_form">
<div class="loginTitle">Client Login</div>
<div class="loginLabel">Username: <span id="userError"><?php echo $test ?></span></div>
<input type="text" name="username" class="loginBox" id="userName">
<div class="loginLabel">Password: <span id="passError"></span></div>
<input type="password" name="password" class="loginBox" id="passWord">
<br>
<input type="checkbox" name="rememberMe" value="rememberMe" id="remMe">Remember me for next time
<br>
<input type="submit" value="Login" name="submit" id="submit">
</form>
login.html-位于顶部
<?php
include ("checkLogin.php");
?>
预期:当我输入错误的用户名时,它应该在表单标签旁边显示“这是一个失败”
实际:没事