【问题标题】:Opening a Modal Window "using" PHP“使用” PHP 打开模态窗口
【发布时间】:2013-02-01 03:26:31
【问题描述】:

我正在用 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 时显示模式窗口?

非常感谢所有 cmets、答案和建议!

谢谢! - 瑞恩

【问题讨论】:

  • 标头位置应使用完整的 URI。
  • 它只是在本地主机上,所以一旦它上线就会改变。
  • 您可以根据 $_GET 值编写一个 javascript。这与这里的问题不相似吗:stackoverflow.com/questions/11594974/…
  • @BharathParlapalli 我认为这不相似。我的要简单得多。我只需要以某种方式使用 PHP 打开一个模式窗口。一定有办法做到这一点!哈哈。我想到了 JavaScript 的事情。老实说,我只是不知道如何.. 由于我不知道,我什至不知道这是否会奏效。

标签: php jquery modal-dialog


【解决方案1】:

好的,所以你现有的代码就像这个 sn-p,你遇到的问题是你想显示一个由/使用 PHP 触发的 jQuery/Bootstrap 模式,但是 jQuery/JS 还没有加载(所以你会得到一个“$ undefined”错误)。

当您说“一个小的弹出窗口”时,我假设您的意思是引导类型模式。

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 ?>

不是 100% 确定这会回答您的问题,但这对我有用。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    首先,

    您不能直接“使用 PHP 打开模式窗口”。 您只能通过交换变量(通过 JSON 或 XML)或将 PHP 条件直接嵌入到您的标记中来做到这一点。

    PHP 和 JavaScript 是独立的。

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

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

    第二:在某个地方隐藏输入,例如 (&lt;input type="hidden" id="showModal" /&gt;),然后通过 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>
    

    【讨论】:

      【解决方案3】:

      我知道这是一个旧帖子,但为了帮助将来有类似任务的人,我想以下内容可能有助于进入正确的方向(我自己测试了下面的代码,所以请进行调整)。

      <?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 } ?>
      

      【讨论】:

        【解决方案4】:

        您是否正在寻找类似的东西:

        <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 中的链接,那么代码将是 &lt;a href="#popout"&gt;Open Popout&lt;/a&gt;,如果有帮助的话。模态窗口看起来更好。 :P
        • 你知道如何创建一个在某些 JS 事件中出现/消失的模态框吗?
        • 此外,您还必须获得 jQuery UI 和 UI CSS 才能显示它。
        • @Class 只是一个问题(我不知道)不应该是$("#dialog").modal();。并且编辑也不起作用。我不知道这是否会有所帮助,但这是我获得模态插件的地方。 alessioatzeni.com/blog/….
        • @RyanFitzgerald 我正在给你一个 jQuery UI 示例,它是 .dialog(); 你得到的网站将与你的网站一样工作,你只需要根据你的外观对其进行修改。跨度>
        【解决方案5】:

        使用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(); 放在文件的顶部。

        【讨论】:

          【解决方案6】:

          或者你甚至可以在 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 的建议有何不同?只是想知道为什么负面评价。
          【解决方案7】:

          请下载模态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..... 
          }
          

          【讨论】:

            猜你喜欢
            • 2016-09-17
            • 1970-01-01
            • 2018-10-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-12-11
            • 1970-01-01
            相关资源
            最近更新 更多