28

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Spoonk
  • 705
  • 2
  • 10
  • 17
  • What do you do if you don't have access to the FORM element and also don't have access to the code of the PARENT page? – Doug Apr 14 '15 at 00:49

9 Answers9

32

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">
Michal M
  • 9,322
  • 8
  • 47
  • 63
  • +1 good solution. However, it will mess up any server-side error checking if you want to re-direct the user to the original page in case of an error. – jeroen Jun 06 '10 at 18:39
  • @jeroen I make a field check with javascript inside my simple .html page. I do not need a redirection in case of empty fields. I use alert :) – Spoonk Jun 06 '10 at 18:53
11

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.

Andrew
  • 18,680
  • 13
  • 103
  • 118
10

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.

Dan Heberden
  • 10,990
  • 3
  • 33
  • 29
4

we can use javascript like this :

target.window.location='locationpage.html';
top.window.location='mypage2.html';
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
bungdito
  • 3,556
  • 4
  • 31
  • 38
1

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

Dimgba Kalu
  • 143
  • 9
1

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>

<? }?>
Jas
  • 11
  • 1
1

In the page that you want to load in _top, add the follow code in the header

<script type="text/javascript">
if (top != self) top.location.href = location.href;
</script>
Doberon
  • 611
  • 9
  • 19
0

For CakePHP 4, to redirect your parent page just add option 'target'=>'_top' in your iframe's link:

Example:

 <?= $this->Html->link(__('Redirect Parent'), ['controller' => 'Users', 'action' => 'view'], ['target'=>'_top']) ?>

All the best!

Marwan Salim
  • 682
  • 7
  • 14
0

You can add multiple headers to your redirect.
I use a little function that I created with this in mind.

public function redirect($url){
   header('Window-target: _top');
   header('Location:' . $url, true, 303);
   exit();
}
denOldTimer
  • 175
  • 1
  • 11