使用PHP打开模态窗口

3

我正在使用PHP创建一个登录系统,并尝试使它看起来更好。

当您注销时,您将被重定向回index.php。就像这样:

header("loaction: index.php?logout=true")

这会使url看起来像www.mysite.com/index.php?logout=true。然后我使用以下代码:
if(isset($_GET['logout'])) {

$logoutvalue = $_GET['logout'];

if($logoutvalue = "true") {
$notification = "You've been logged out!";  
}

从URL中获取logout的值并对其进行处理。

我有一个小弹出窗口,将显示通知变量的值。在这种情况下,它是“您已注销!”。我的问题是,如何在页面加载时,并且当url等于/index.php?logout=true时,让模态窗口显示?

非常感谢所有的评论、答案和建议!

谢谢! - Ryan


头部位置应使用完整的URI。 - user557846
1
这只是在本地主机上,一旦上线就会改变。 - Ryan Fitzgerald
你可以根据 $_GET 的值编写一个基于 JavaScript 的程序。这不是和这里的问题类似吗:http://stackoverflow.com/questions/11594974/pop-up-window-based-on-php-input-value - Bharath Parlapalli
@BharathParlapalli 我不认为它们相似。我的要简单得多。我只需要用PHP打开一个模态窗口就行了,肯定有某种方法可以实现!哈哈。我也想过JavaScript的事情,但是老实说我不知道该怎么做..而且由于我不知道,我甚至不知道那是否可行。 - Ryan Fitzgerald
7个回答

4

首先,你不能直接使用PHP打开模态窗口。你只能通过交换变量(通过JSON或XML)或将PHP条件嵌入到标记中来实现。

PHP和JavaScript是独立的。

我的问题是如何在页面加载时显示模态窗口,以及当URL等于/index.php?logout=true时如何显示?

有两种方法可以实现这个目标。
第一种方法:充分利用将PHP条件嵌入到标记中。

第二种方法:在某个地方放置隐藏输入框,例如(<input type="hidden" id="showModal" />),然后通过JavaScript自身检查它是否存在。

例如:

<!DOCTYPE html>
<html>
<head>

   <script type="text/javascript">

      window.onload = function(){

           if ( document.getElementById('showModal') ){

               alert('Box'); //replace with your own handler
           }

      }

   </script>

</head>
<body>

<?php if ( isset($_GET['logout']) && $_GET['logout'] === 'true' ): ?>

<input type="hidden" id="showModal" />

<?php endif;?>
</body>
</html>

2

我知道这是一篇旧文章,但为了帮助未来遇到类似问题的人,我想以下内容可能有助于找到正确的方向(我已经测试过下面的代码,请根据需要进行调整)。

<?php if(isset($_GET['logout']) && $_GET['logout'] === 'true'){
        $message = "You've been logged out!";
    ?>
        <script>
        $(function() {
         $('#myModal').modal('show');
        });
        </script>

    <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Edit Data</h4>
                </div>
                <div class="modal-body">
                    <div class="fetched-data"><?php $message ?></div> 
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

    <?php } ?>

2
您是否在寻找类似以下内容的东西:
<script>
<?php if(isset($_GET['logout']) && $_GET['logout'] === 'true'){
    echo 'alert("You\'ve been logged out!");'
}
?>
</script>

编辑:我认为您正在寻找类似于此的模态窗口(使用jQuery)

<?php if(isset($_GET['logout']) && $_GET['logout'] === 'true'){
    $message = "You've been logged out!";
?>
    <script>
    $(function() {
        $("#dialog").dialog();//if you want you can have a timeout to hide the window after x seconds
    });
    </script>
    <div id="dialog" title="Basic dialog">
        <p><?php echo $message;?></p>
    </div>
<?php } ?>

不完全是。我不想要一个JavaScript警告框。我想要显示模态框。所以,如果我在HTML中点击一个链接,那么代码应该是<a href="#popout">打开弹出窗口</a>,如果这有帮助的话。模态窗口看起来更漂亮。 :P - Ryan Fitzgerald
你知道如何创建一个模态框,它会在某些JS事件上出现/消失吗? - rlatief
此外,您还需要获取 jQuery UI 和一个 UI CSS 来显示它。 - Class
@Class 只是一个问题(我不知道)应该是 $("#dialog").modal();。而且编辑也没有起作用。我不知道这是否有帮助,但这是我得到模态插件的地方。http://www.alessioatzeni.com/blog/login-box-modal-dialog-window-with-css-and-jquery/. - Ryan Fitzgerald
@RyanFitzgerald,我给你提供了一个jQuery UI的示例,即.dialog();。你得到的网站也可以使用相同的方法,只需根据你想要的外观进行相应的修改即可。 - Class

1

好的,您现有的代码如下所示,您遇到的问题是希望使用PHP触发/显示jQuery/Bootstrap模态框,但是jQuery/JS尚未加载(因此会出现“$未定义”错误)。

当您说“一个小弹出窗口”时,我假设您指的是Bootstrap类型的模态框。

if(isset($_GET['logout'])) {
  $logoutvalue = $_GET['logout'];

  if($logoutvalue = "true") {
    $notification = "You've been logged out!";  
  }
}

我所做的是将PHP代码块移动到.php文件的末尾,在jQuery(等)之后进行包含,这样它就会像这样看起来(假设您的模态框名称为id="logoutModal"):

logout.php

if(isset($_GET['logout'])) {
  $logoutvalue = $_GET['logout'];

  if($logoutvalue = "true") {
    include("logout-modal.php");  
  }
}

logout-modal.php

<?php ?>
  <script>
    $('#logoutModal').modal('show');
  </script>
<?php ?>

我不确定这是否完全回答了你的问题,但这对我有用。希望能对你有所帮助。


0

使用PRG模式是完成此任务的最佳方式。

index.php

1. 在HTML中创建模态框内容的元素

<div id=modal></div>

2. 为你的 #modal 元素添加样式

#modal {
    position: fixed;
    display: none; /*important*/
    color: white;
    text-align: center;
    z-index: 1000;
    width: 100%;
    height: 80px;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    background: crimson;
    line-height: 2;
}

3. 使用jQuery定义函数来显示和隐藏模态框

'use strict'

window.jQuery ?

    $.fn.notify = function(interval) {      
        let el = $(this),
            text = el.text(),

            modal =()=> { // this will fire after the interval has expired
                el.fadeOut(),
                clearInterval(timer)
            },

            timer = setInterval(modal,interval)

        !text.match(/\S+/) || el.text('') // this will fire when modal box contains text    
            .append(`<h3>${text}</h3>`)
            .show()
    }

: alert('jQuery required.')

4. 将 .notify() 附加到 jQuery 选择器

$(()=>{
    $('#modal').notify(1500) // 1500 is the interval in milliseconds
})

5. 将从 PHP 发送的通知插入到 #modal 元素中

<div id=modal>
    <?=
        @$_SESSION["notify"]; // if this variable is not set, nothing will happen
        unset($_SESSION["notify"]); 
    ?>
</div>

6. 执行一个简单的 $_GET 请求

<a href=parse.php?logout>Logout</a>


parse.php

7. 使用 PHP 在 $_SESSION 变量中返回一个值

if (isset($_GET["logout"])) {
    $_SESSION["notify"] = "You have been logged out.";
    header("Location: index.php");
}


这是一个好的实践。一旦注销请求被发送到PHP,它将通过会话变量重定向到主页并显示结果。PRG模式并不总是需要POST请求。此示例发送GET请求,适用于注销操作。请注意,在文件顶部必须放置session_start();


-1

或者你甚至可以在 if 语句内部编写脚本:

<?php
if(isset($_GET['logout']) && $_GET['logout'] === 'true'){ ?>
    <script type="text/javascript>
     window.open(yourhtml);
 </script>
</php } 
else {
// may be some other script
}
?>

这与@Class建议的有何不同?只是想知道为什么会有负面评价。 - Bharath Parlapalli

-1
请下载一个模态框JS库http://simplemodal.plasm.it,并使用以下条件。
if(isset($_GET['logout'])) {

$logoutvalue = $_GET['logout'];

if($logoutvalue = "true") {
code to open the Modal as in the JS library..... 
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接