弹出没有显示! PHP回音+警报

时间:2015-02-25 13:58:10

标签: 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>"; 


   ?>

1 个答案:

答案 0 :(得分:1)

您必须先发送标头。所以只需切换这些行:

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>