根据$ _GET显示/隐藏Div

时间:2016-07-19 14:11:38

标签: javascript php jquery twitter-bootstrap-3

我目前正在使用Bootstrap 3 Alert div在事务完成时刷新消息。警报内容来自上一页的标题(位置)。

在结束/正文标记上方:

</script>
<script type="text/javascript">
    window.setTimeout(function() {
    $(".alert").fadeTo(500, 0).slideUp(500, function(){
        $(this).remove();
    });
}, 4000);
</script>

我的警告:

<div class="bs-example">
    <div class="alert alert-success" id="success-alert" style="font-size:120%;">
        <a href="#" class="close" data-dismiss="alert">&times;</a>

    <?php

        $email = $_GET['e'];
        $receipt = $_GET['r'];

    if (isset($_GET["msg"])) {

        $msg = $_GET["msg"];

        if ($msg == "1") {

            echo "Receipt $receipt was sent to $email.";
        } else {

            echo "Message was not sent.";
        }
    }
    ?>

    </div>
</div>

即使div不存在,警报$_GET也会显示。换句话说,在我做任何事情之前加载div是空白的。我希望用户在我的页面上填写表单,并将URL?msg = 1重定向到同一页面,并显示Alert消息。除此之外,div不必显示。

1 个答案:

答案 0 :(得分:5)

您需要将警报代码包装在PHP if语句中,您可以在其中检查是否设置了$ _GET变量。

 <?php if(isset($_GET['e']) && isset($_GET['r'])) { ?>

   <div class="bs-example">
        <div class="alert alert-success" id="success-alert" style="font-size:120%;">
            <a href="#" class="close" data-dismiss="alert">&times;</a>

        <?php

            $email = $_GET['e'];
            $receipt = $_GET['r'];

        if (isset($_GET["msg"])) {

            $msg = $_GET["msg"];

            if ($msg == "1") {

                echo "Receipt $receipt was sent to $email.";
            } else {

                echo "Message was not sent.";
            }
        }
        ?>

        </div>
    </div>

 <?php } ?>