如何在成功登录时关闭父级iframe?

时间:2013-02-25 02:35:39

标签: php jquery iframe fancybox parent

我正在尝试在iframe中打开登录表单。如果用户单击“提交”按钮时登录成功,我希望关闭iframe并将浏览器重定向到index.php。 问题是我不知道如何从那时起关闭iframe。

以下是与iframe相关的代码:

login.tmpl(html文件,表格所在的位置)

<div class="form_item submit_button">
  <button type="submit" name="submit">Login</button>
</div>

login.php(我重定向到索引。这是我认为我需要一些额外的代码来关闭iframe)

  if (!$error) {
        throw new RedirectBrowserException('index.php');
  }

gallery.tmpl(调用iframe。我认​​为不应该重要)

<a class="fancybox iframe fancybox.iframe" href="login.php">Iframe Login</a>

编辑。这是我的jQuery代码(gallery.tmpl):

        $(document).ready(function() {
          $(".fancybox").fancybox({
            padding : 0,
            prevEffect : 'none',
            nextEffect : 'none',
            helpers : {
                title : {
                  type: 'over'
                },
                thumbs : {
                  width : 50,
                  height : 50
                }
            }
          });
        });
        $(document).ready(function() {
          $("a.iframe").fancybox({
            autoDimensions : false,
            autoSize : false,
            padding : 0,
            width : 395,
            height : 195,
            type : 'iframe'
          });
        });

2 个答案:

答案 0 :(得分:0)

您可以从打开fancybox( gallery.tmpl )的页面重定向到 index.php 。只需在您的fancybox自定义脚本中添加回调:

$(".fancybox").fancybox({
   afterClose : function(){
      // redirects to another file after closing
      window.location.href = "index.php";
   }
});

然后,在 loging.php 内,你可以这样做,以便在成功时关闭fancybox:

<?php if (!$error) { ?>
  <script>parent.jQuery.fancybox.close();</script>
<?php }; ?>

答案 1 :(得分:0)

刚刚解决了问题。最后,这使它成功:

  if (!$error) { ?>
    <script type="text/javascript">
    parent.$.fancybox.close();  //can use jQuery instead of $
    window.parent.location ='index.php';    
    </script> <?php
  }

所以,重新定向父母是我失踪的东西,直到我刚才想出来。我一开始尝试使用某种延迟,但是没有用。