PHP "header (location)" inside IFRAME, to load in _top location?
Asked Answered
M

9

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.

Manservant answered 6/6, 2010 at 18:28 Comment(1)
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?Mimetic
R
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">
Roadstead answered 6/6, 2010 at 18:30 Comment(2)
+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.Loafer
@Loafer 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 :)Manservant
S
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.

Script answered 9/2, 2016 at 20:43 Comment(0)
F
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.

Footmark answered 6/6, 2010 at 18:36 Comment(0)
K
4

we can use javascript like this :

target.window.location='locationpage.html';
top.window.location='mypage2.html';
Kavita answered 20/4, 2011 at 1:54 Comment(0)
B
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

Barrio answered 18/7, 2012 at 11:15 Comment(0)
K
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>

<? }?>
Kweiyang answered 15/11, 2014 at 17:36 Comment(0)
S
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>
Soapberry answered 3/6, 2019 at 17:6 Comment(0)
L
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!

Lalo answered 28/7, 2020 at 4:6 Comment(0)
C
-1

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();
}
Cypriot answered 3/2, 2023 at 17:44 Comment(1)
@MarkRotteveel Sorry didn't have much time when searching for a front-end solution to the same problem.Cypriot

© 2022 - 2024 — McMap. All rights reserved.