PHP “header (location)” inside IFRAME, to load in

2019-01-11 22:21发布

I have a simple form which is inside IFRAME. When user click on SUBMIT, it redirects to a specific page on my server. The function I use for the redirect is

 header ('Location: mypage2.html');
exit ();

But I want the new page to open in _top location, not inside the same IFRAME that I use. How can I tell the browser to open the new page in _top not inside the IFRAME? Thanks in advance.

6条回答
Root(大扎)
2楼-- · 2019-01-11 22:49

we can use javascript like this :

target.window.location='locationpage.html';
top.window.location='mypage2.html';
查看更多
Emotional °昔
3楼-- · 2019-01-11 22:53

You can use javascript to access the parent. You could echo out javascript in your PHP.. so your parent page has this:

function changeURL( url ) {
    document.location = url;
}

and in your php script, you echo

<script>
   parent.changeURL('mypage2.html' );
</script>

The reason you can't call parent.document.location is because it's read only - you have to have a function available on the parent to do it.

查看更多
在下西门庆
4楼-- · 2019-01-11 23:00

You can either link to the page using <a href="pagename.php?Break=Y">Break Out</a> or use code

<?php header("Location: pagename.php?Break=Y"); ?>

You then use the following code in the header of the page with

if(isset($_GET['Break'])) // 
    {
    $BreakFrame = true;
    $BreakToPage = "pagename.php";

    }

<script language="JavaScript" type="text/javascript">
function changeURL( url ) {
    document.location = url;
}
</script>

<?php if($BreakFrame) { ?>

<script language="JavaScript" type="text/javascript">
parent.changeURL('<?=$BreakToPage?>' );
</script>

<? }?>
查看更多
爷、活的狠高调
5楼-- · 2019-01-11 23:04

A simple way directly in PHP to redirect the parent page rather than the iframe:

echo "<script>top.window.location = '/mynewpage.php'</script>";
die;

The die; isn't necessarily necessary, but it is good "just in case" to prevent the script from continuing any further, for example, if javascript is disabled in the user's browser.

查看更多
你好瞎i
6楼-- · 2019-01-11 23:10

You are not able to achieve the desired effect in PHP. This is something you'd have to do from JavaScript or add target attribute to <form>:

<form ... target="_top">
查看更多
可以哭但决不认输i
7楼-- · 2019-01-11 23:15

The best and simplest thing to do is to use the form target element

<form action="..." target="_top"> 
<form action="..." target="_parent">

either of the target parameters works fine

查看更多
登录 后发表回答