【问题标题】:Alert or Message Box are not popping up after an IF/Else Condition出现 IF/Else 条件后未弹出警报或消息框
【发布时间】:2019-03-08 18:15:30
【问题描述】:

代码本身可以工作,但我唯一的问题是警告框。下面是我的代码,它确认表中的 categoryID 是否与另一个表的 categoryID 相同。我担心的是。我的变量$messageecho "<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


【解决方案1】:

使用 javascript 重定向到“addpost.php”:

echo "<script type='text/javascript'>
alert('$message');
window.location.href = 'addpost.php';
</script>";

【讨论】:

    猜你喜欢
    • 2017-02-13
    • 2012-08-22
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 2020-08-09
    • 2015-08-07
    相关资源
    最近更新 更多