如果出现其他情况,则不会弹出“警报”或“消息框”

时间:2019-03-08 18:15:30

标签: javascript php html

代码本身正在运行,但是我唯一的问题是警报框。下面是我的代码,用于确认表中的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> 

1 个答案:

答案 0 :(得分:0)

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

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