1

Let's say i got a link on a homepage, and an iFrame on the following page.

Now i need to make that link on the homepage to pass an URL to the iFrame on the following page (on click), so the iFrame can load that URL.

How would i approach this without using any JS?

I know how to do this on a single page, where i click a link and just target the iFrame. But doing this over two different pages (passing the URL) is too high for me.

Thanks in advance!

Joe
  • 1,047
  • 1
  • 13
  • 25

4 Answers4

3

Roughly said:

Homepage:

<a href="nextpage.php?link=http://example.com">nextpage</a>

Nextpage.php:

<iframe src="<?php echo $_GET['link'];?>"></iframe>
bogatyrjov
  • 5,317
  • 9
  • 37
  • 61
2

You basically have two ways you can go:

Simply create a link that has the link as a query string

<a href="page_2.php?link=mylinkusediniframe.com">Cick</a>

And than check the link in your PHP file:

$link = $_GET['link'];
if (! preg_match("^(?:https?://)?(?:[a-z0-9-]+\.)*((?:[a-z0-9-]+\.)[a-z]+)", $link) ) {
    $link = null;
}

And finally print it:

<iframe src="<?php print $link; ?>">

The regex is nicely explained where I've found it: https://stackoverflow.com/a/10435670/1456376


An alternative would be:

Store the URL in the SESSION on the first page

The advantage is, that the user can't manipulate the URL displayed (as he can with the first solution by simply changing the URL in the address bar).

In the first file do:

$_SESSSION['link'] = 'http://mylink.com';

In the second simply output it:

<iframe src="<?php print $_SESSION['link']; ?>">
Community
  • 1
  • 1
insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
1

Edit: I do not understand if the window with the iframe is already open or not. If it is already open then you will want my method, if not read Jevgeni Bogatyrjov method as that is the correct one. You will want to look into Javascript, as PHP will not help you with that task. First you will need to open the new window with javascript to begin with, like so:

new_window = window.open("page2.php", "window2", "height=120");

Then, you can call javascript functions from 1 window to the other like

new_window.test();

So if the new page had an iframe, using javascript you can change the iframes source with a function like

<a href="#" onlclick="new_window.test('newpage.html');">Link</a>

On the other page you will create a function called test with a url paramater, and then change the iframes source with the new parameter.

Joe
  • 1,047
  • 1
  • 13
  • 25
1

you can pass the address from homepage to iframe by Get method. This is the page with the iframe: http://www.page-with-iframe.php This is the url you want to pass: http://www.url-for-iframe.php

You can add a link in homepage with the "url-for-iframe" encoded with the function base64_encode. The link will be like this:

http://www.page-with-iframe.php?link=2jhGe2R6Wd5nJcrJgLgFrS7dCWiloJKf

You can use base64_decode in the page with iframe and pass to it the url obtained.

Bye

fonta7
  • 64
  • 1
  • 4