Page 1 of 1

Accessing global vars from iframes

Posted: Tue Sep 02, 2008 1:45 pm
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

Re: Accessing global vars from iframes

Posted: Tue Sep 02, 2008 2:13 pm
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

Re: Accessing global vars from iframes

Posted: Fri Sep 05, 2008 12:24 pm
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

Re: Accessing global vars from iframes

Posted: Fri Sep 05, 2008 1:18 pm
by andyhoneycutt
Maybe remove the target from your link, or make sure you are targeting the correct window?