Javascript php脚本中的Javascript警报

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8032072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:27:29  来源:igfitidea点击:

Javascript alert in php script

phpjavascript

提问by user893970

I wanted to popup an alert box. After that, the site would redirect to main page. But, it seems that it directly redirect to the mainpage without alerting anything.

我想弹出一个警告框。之后,该站点将重定向到主页。但是,它似乎直接重定向到主页而不发出任何警报。

if($registerquery)
    {
    ?>

    <script>alert('received!')</script>
<?php
    }


    header("Location: mainpage.php");
    exit();


    ?>

I wanted to do this to ensure users that the process of submission ended successfully. How can i alert something before the page redirect to mainpage and more importantly what causes this? I think the page should not have redirected before the alert box.(Before these codes, site registers what users submitted but not relevant i guess.)Thanks

我想这样做是为了确保用户提交过程成功结束。如何在页面重定向到主页之前发出警报,更重要的是是什么导致了这种情况?我认为页面不应该在警告框之前重定向。(在这些代码之前,站点注册了用户提交但不相关的内容,我猜。)谢谢

回答by Niet the Dark Absol

You just can't do this. PHP is server-side, JS is client-side. Using a location header is server-side, so the browser never gets the JS.

你不能这样做。PHP是服务器端,JS是客户端。使用位置标头是服务器端的,因此浏览器永远不会获取 JS。

Instead, try something more like:

相反,尝试更像:

if( $registerquery)
    echo "<script>alert('received!'); location.href='mainpage.php';</script>";

and remove the headerbit altogether.

header完全删除该位。