为什么我的回声没有';t在头函数头(位置:home.php)之前工作;


why my echo doesn't work before a header function header(location:home.php);

为什么我的echo在这段代码中不起作用?它直接进入函数头()。

if($result){
    echo("<script> alert('User Successfull Added')</script>");
    header('location:home.php');
}

为了让你的代码示例工作,如果你想让它工作,你需要把它放在if/else条件中,但由于我只看到你发布的代码,所以我把它作为一个示例:

<?php
if($result){
    echo("<script>alert('User Successfully Added')</script>");
    echo("<script>window.location = 'home.php';</script>");
}
else {
    header('Location: elsewhere.php');
}

实际上你的回声在起作用。但是我们看不到消息,因为同时页面重定向到home.php页面。

如果您想提醒以显示提醒消息,请使用jquery post。或php会话变量

假设您希望同时显示警报&重定向到home.php,为什么不尝试设置SESSION(或其他全局变量),然后检查它是否设置在重定向页面的开头?

例如:

<?php
    session_start();
    $_SESSION['usercreated']="Y";
    header('Location: elsewhere.php');
?>

然后在home.php上的php代码开头:

<?php
    session_start();
    if(isset($_SESSION['usercreated'])){
        echo("<script> alert('User Successfully Added')</script>");
    }
    //....
?>

对不起,我不能在这里添加评论。但我认为你想做的需要Java,并用confirm()代替alert()

var r=confirm("User Successfull Added")
  if (r==true) // if user click OK
    {
      window.location.assign("home.php")
    }
      else // user click Cancel
    {
    }