Still can't get the iframe to display

I had a previous post and it sort of died. I have tried every combination of concatenation using quotes and double quotes to create a php page and insert a php file using a variable into an iframe. I can get the iframe to appear but the only content I get inside the iframe is the foot of the site as I try to escape the quotation marks. My latest round of attempts is to create the string in the variable and then echo the string. Does anyone have additional suggestions?

<?php $fname = "http://thisisthe.pdf"; //create variable $iframetxt = "<iframe src = \' $fname \' "; //build iframe echo $iframetxt; //echo iframe ?>

You can try using double quotes for the string that contains the iframe tag and escape the double quotes inside the attribute value with a backslash. Here’s an example:

<?php 
$fname = "http://thisisthe.pdf"; //create variable 
$iframetxt = "<iframe src=\"$fname\"></iframe>"; //build iframe 
echo $iframetxt; //echo iframe 
?>

Alternatively, you can use single quotes for the string and concatenate the variable using the concatenation operator .. Here’s an example:

<?php 
$fname = "http://thisisthe.pdf"; //create variable 
$iframetxt = '<iframe src="' . $fname . '"></iframe>'; //build iframe 
echo $iframetxt; //echo iframe 
?>

Both of these methods should work without any issues. Just make sure that the value of $fname is a valid URL.

Thanks Toby. I tried both methods and keep getting my home page but not the pdf in the frame. I tested the pdf by copy and paste and it works. For whatever reason instead of escaping the double quotes I just get the default. I just stuck some files in a directory behind my actual site that is on wordpress. Do you think this could be the issue?

Is your script where you want to display the Iframe for the PDF on an other site?
Then maybe you have some Cross-Origin Issues.

If you want to embed a website into your own website, you need to have control over the website’s domain in order to modify or remove its CORS policy. However, if the website has explicitly blocked requests from other domains, there is no way to bypass this restriction.

This security feature is in place to prevent unauthorized use of a website by displaying it in an iframe on another website. For example, someone could display Google in an iframe on their own website with their own ads on top, which would be unfair to Google.

Although that may be an unyet discovered issue with what I am doing, it may not be the issue now. I was hoping to combine a number of pdf files at once to read them and play them. When testing your last idea, I replaced my remote address with a local pdf file. Would my site be considered remote if my URL is absolute rather than relative to the code location? I will test it when I return on Friday. Thanks for the insight.

CORS must be the issue. I am able to use a relative link to a file on my own site and can display it in the iframe. I am not able to use a full URL or one to another site.

Sponsor our Newsletter | Privacy Policy | Terms of Service