Accessing global vars from iframes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
neridaj
Forum Commoner
Posts: 40
Joined: Fri Jan 05, 2007 9:55 pm

Accessing global vars from iframes

Post by neridaj »

Hello,

I have an iframe contained within a page whose source page includes a call to a function that accesses a $_GET[] superglobal. is there some sort of "parent" function I need to call to have this var accessible to the iframe calling function? The iframe src page works without relying on variables i.e., the page is populated as expected with thumbnails, however, I need to separate which thumbnails are loaded based on the $_GET[] var.

Code: Select all

 
// iframe src - thumbs.php
<?
get_main_thumbs();
?>
 
// page containing iframe - portfolio.php
<div id="thumbs">
<iframe id="ifrm" name="ifrm" src="thumbs.php" scrolling="auto"
width="600" height="94" frameborder="0">
[Content for browsers that don't support iframes goes here.
Suggestion: include link to file specified in iframe src attribute.]
</iframe>
</div>
 
// function
function get_main_thumbs()
{
    $projecttype = $_GET['pt'];
    $dir = '../projects/' . $projecttype . '/';
    echo $dir;
    $files = scandir($dir);
    foreach($files as $value) {
        if(!in_array($value, array('.', '..'))) {
    
            // check for folders
            if(is_dir($dir.DIRECTORY_SEPARATOR.$value)) {
 
                printf('<div class="thumb"><a href="portfolio.php?pt=%s&pn=%s" target="contents">'.
                    '<img src="' . $dir . $value . '/after/' . $value . '01.jpg" width="50" height="50" /></a></div>', $projecttype, urlencode($value));
            }
        }
    }
}
 
Thanks,

Jason
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Accessing global vars from iframes

Post by andyhoneycutt »

Code: Select all

 
// page containing iframe - portfolio.php
<div id="thumbs">
<iframe id="ifrm" name="ifrm" src="[color=red]thumbs.php[/color]" scrolling="auto"
width="600" height="94" frameborder="0">
...
 
You'll need to pass the $_GET vars through the iframe by way of writing them into the url for the iframe, e.g.:

Code: Select all

 
$get_string = "";
foreach($_GET as $key => $value)
{
  $get_string .= "$key=$value&";
}
$get_string=rtrim('&',$get_string);
 
In your iframe:

Code: Select all

 
<iframe id="ifrm" name="ifrm" src="[color=green]thumbs.php[b]?<?php echo $get_string; ?>[/b][/color]" scrolling="auto" width="600" height="94" frameborder="0">
 
-Andy
neridaj
Forum Commoner
Posts: 40
Joined: Fri Jan 05, 2007 9:55 pm

Re: Accessing global vars from iframes

Post by neridaj »

Thanks for the reply. I did this which works, but when I click on the thumbnails everything is loaded into a new window.

Code: Select all

 
function get_main_thumbs()
{
    $projecttype = $_GET['pt'];
    $projectname = $_GET['pn'];
    $dir = '../projects/' . $projecttype . '/';
    $files = scandir($dir);
    foreach($files as $value) {
        if(!in_array($value, array('.', '..'))) {
    
            // check for folders
            if(is_dir($dir.DIRECTORY_SEPARATOR.$value)) {
 
                printf('<div class="thumb"><a href="portfolio.php?pt=%s&pn=%s" target="contents">'.
                    '<img src="' . $dir . $value . '/after/' . $value . '01.jpg" width="50" height="50" /></a></div>', $projecttype, urlencode($value));
            }
        }
    }
}
 
How can I make the link just load a the data in the same browser window?

Thanks,

Jason
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Accessing global vars from iframes

Post by andyhoneycutt »

Maybe remove the target from your link, or make sure you are targeting the correct window?
Post Reply