弹出不显示! PHP回声+警报

Pop up not showing! PHP echo+alert

有人可以向我解释为什么我的警报不起作用吗?前几天还好好的,突然不显示了,问题是只有一个坏了

请看下面的代码,IF(){ 内部的 "echo ..." 不起作用,而 IF(){ 外部的 "echo..." 有效。

我已经记不清我通过这段代码多少次了,但什么也找不到!

除了使用 echo+alert 之外,还有更好的方式向用户显示弹出消息吗?

    <?php
        if ($cobrar_mensalidade === 'sim') {

            //sending email
                $mensagemHTMLinscrito = 'Hello';

                //headers
                $headers2 = "MIME-Version: 1.1" . $quebra_linha;
                $headers2 .= "Content-type: text/html; charset=iso-8859-1". $quebra_linha;
                $headers2 .= "From: " . $emailSender . $quebra_linha;
                $headers2 .= "Return-Path: " . $emailSender . $quebra_linha;
                $headers2 .= "Reply-to: " .  $emailSender . $quebra_linha;

                //sending method
            mail($email, 'bla', $mensagemHTMLinscrito, $headers2, $emailSender);

            echo "<script type=\"text/javascript\">alert('Not working');</script>";

            header("Location: http://www.blabla.com");
            die("just die");
        }


        //sending email for other people

                $mensagemHTMLinscrito = 'bla bla';

                //headers
                $headers2 = "MIME-Version: 1.1" . $quebra_linha;
                $headers2 .= "Content-type: text/html; charset=iso-8859-1". $quebra_linha;
                $headers2 .= "From: " . $emailSender . $quebra_linha;
                $headers2 .= "Return-Path: " . $emailSender . $quebra_linha;
                $headers2 .= "Reply-to: " .  $emailSender . $quebra_linha;

                //sending method
            mail($email, 'bla bla bla', $mensagemHTMLinscrito, $headers2, $emailSender);

        echo "<script type=\"text/javascript\">alert('This works');</script>"; 


   ?>

你必须先发出headers。所以只需切换这些行:

echo "<script type=\"text/javascript\">alert('Not working');</script>";
header("Location: http://www.blabla.com");

至:

header("Location: http://www.blabla.com");
echo "<script type=\"text/javascript\">alert('Not working');</script>";

这将修复您的重定向。但是我不认为脚本会被执行,因为浏览器会直接加载目标站点。您可能更喜欢这个简单的脚本:

<script type="text/javascript">
alert('Not working');
location.replace("http://www.blabla.com");
</script>