【发布时间】:2019-03-08 18:15:30
【问题描述】:
代码本身可以工作,但我唯一的问题是警告框。下面是我的代码,它确认表中的 categoryID 是否与另一个表的 categoryID 相同。我担心的是。我的变量$message 和echo "<script type='text/javascript'>alert('$message');</script>"; 在进入该位置之前不会弹出。我想要的是当两个 categoryID 不匹配时。我想在进入header("Location:addpost.php");位置之前弹出一条警报消息(例如“NO ID MATCHED”)
但是当我删除header("Location:addpost.php"); 并让echo "<script type='text/javascript'>alert('$message');</script>"; 保留时。它会弹出。
感谢您的帮助!
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
include "dbconnect.php";
$message = "wrong answer";
if (isset($_GET['categoryID'])) {
$_SESSION['postCategory']['categoryID'] = $_GET['categoryID'];
}
$que = "SELECT * FROM `post`";
$res = $con->query($que);
while ($row = $res->fetch_assoc()) {
if ($row['categoryID'] == $_SESSION['postCategory']['categoryID']) {
header("Location:postCategory.php?categoryID=".$_SESSION['postCategory']['categoryID']);
} else {
echo "<script type='text/javascript'>alert('$message');</script>";
header("Location:addpost.php");
}
}
?>
</body>
</html>
【问题讨论】:
-
您似乎没有启用 PHP 错误。
header("Location:addpost.php");应该会抛出一个错误。 -
如果你的服务器告诉页面重定向,不应该有任何数据应该首先显示。期望浏览器得到重定向响应,并立即重定向。
-
回答你的问题
header("Location:addpost.php");应该是第一个也是唯一一个输出到浏览器的,在任何JS或HTML之前,否则这会在PHP中产生错误。这是一个标头重定向,它不会在浏览器中打印任何数据,而是告诉浏览器可以在不同的 URL 找到所请求的页面 -
有没有可能。当我单击 [OK] 按钮时,带有 [OK] 按钮的消息框。这只是它会将其重定向到
header("Location:addpost.php");的时间吗? -
这需要在浏览器中编码,即像 JavaScript 这样的客户端。这不是你会用 PHP 做的事情
标签: javascript php html