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
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>
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">
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>
<? }?>
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.
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!
we can use javascript like this :
target.window.location='locationpage.html';
top.window.location='mypage2.html';