我写了一个简单的登录脚本,连接到一个数据库,现在我想在我的#loginbox中插入一个jQuery段落,当
if (!$row = mysqli_fetch_assoc($result))
是正确的。
我想:
[function.js]
function loginfailed() {
$('#loginbox').html("<p>Login failed.</p>");
}
[下]
<head>
<script type="text/javascript" src="functions.js"></script>
</head>
<?php
include '../config.php';
include 'dbh.php';
session_start();
$uid = $_POST['uid'];
$pw = $_POST['pw'];
$sql = "SELECT * FROM user WHERE uid='$uid' AND pw='$pw'";
$result = mysqli_query($conn, $sql);
if (!$row = mysqli_fetch_assoc($result))
{
header('Location: ../index.php');
echo '<script> loginfailed(); </script>';
}
else
{
header('Location: ../index.php');
}
?>
但是它不起作用。
永远不要以明文形式存储密码!!
关于你的问题。
头函数重定向到index.php,不执行回显。一种解决方案是添加一个$_GET参数,并在重定向后检查它是否存在,并回显消息或添加JS。
if (!$row = mysqli_fetch_assoc($result))
{
header('Location: ../index.php?status=fail');
}
在底部的index.php
文件中(如果你想使用JS/jQuery来显示消息)
<script>
var status = "<?php echo (!empty($_GET['status']) && $_GET['status'] === 'fail') ? 0 : 1; ?>";
if(!status) loginfailed();
</script>
谢谢大家,但是我在Allkin的帮助下找到了我自己的解决方案。
我的头现在重定向到
header('Location: ../index.php?status=fail');
和我的#loginbox检查状态是否设置,然后执行我的loginfailed()函数。
if(isset($_GET['status'])) {
echo '<script> loginfailed(); </script>';
}
那可不容易!
谢谢大家的帮助。