8 Replies - 8300 Views - Last Post: 26 March 2012 - 03:12 AM Rate Topic: -----

#1 deanna.c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-February 12

load external html file into div with php

Posted 25 March 2012 - 09:33 AM

On a site i am making i have 3 sections that are the same on every page. I want to load those sections from an external html file with php on each page so if i want to change the content i only have to do it in 1 place, not on every single page. I am having troubles getting the page to load though. For testing it i am just useing this:

This is my main html file:
<html>
<head>
<title>trial</title>
</head>
<body>
<div id = "contentdiv">

<?php
$dom=new DOMDocument();
$dom->loadHTMLFile('externalhtml.htm');
echo $dom->saveHTML();
?>

</div>
<div>
this is not an external page
</div>	
</body>
</html>



I have also tried this:
<?php
$content = file_get_contents('textfile.txt');
?>

<script type="text/javascript">
div = document.getElementById('contentdiv');
div.innerHTML = '<?php echo $content; ?>';
</script>



and just this:
	
$webDoc = DOMDocument::loadHTMLFile("notReg.html");
print $webDoc->saveHTML();



my externalhtml.html file is just the text "this is an external html file." I read somewhere that having the html, head, and body tags messed it up so i don't have them anymore.

Any help would be much appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: load external html file into div with php

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: load external html file into div with php

Posted 25 March 2012 - 09:45 AM

Why not just use include('externalfile.htm');?

<html>
<head>
<title>trial</title>
</head>
<body>
<div id = "contentdiv">

<?php include('externalhtml.htm'); ?>


</div>
<div>
this is not an external page
</div>	
</body>
</html>



Was This Post Helpful? 0
  • +
  • -

#3 deanna.c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-February 12

Re: load external html file into div with php

Posted 25 March 2012 - 10:19 AM

i tried that as well and its not working. The only thing displayed on the page is the text on the main html.

i tried that as well and its not working. The only thing displayed on the page is the text on the main html.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: load external html file into div with php

Posted 25 March 2012 - 10:27 AM

What is the file name of this main file (the one in which you're trying to include an external file)?
Was This Post Helpful? 0
  • +
  • -

#5 deanna.c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-February 12

Re: load external html file into div with php

Posted 25 March 2012 - 10:46 AM

html.html <- page that you open in your web browser

externalhtml.html <- page that i want to load in html.html

current code for html.html:

<html>
<head>
<title>trial</title>
</head>
<body>
<div id = "contentdiv">

<?php
include('externalhtml.html');
?>

</div>
<div>
this is not an external page
</div>	
</body>
</html>



current content in externalhtml.html:

this is an external html file



I uploaded them to a free host site that does support php and the pages loads with just:

this is not an external page


You can view it here: http://cstwebdev.cla...m.com/html.html
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: load external html file into div with php

Posted 25 March 2012 - 10:49 AM

Quote

I uploaded them to a free host site that does support php


If they don't support PHP, how can you expect PHP to work???? :whistling:

Even if they DID support PHP, the file would need to have a .php extension in order for the web server to know to use PHP to interpret and process the contents of the file.
Was This Post Helpful? 0
  • +
  • -

#7 deanna.c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 08-February 12

Re: load external html file into div with php

Posted 25 March 2012 - 10:57 AM

i said "that does" i know it wouldn't work if they didn't support php..

also the file extension was the problem.. i thought that you needed .php when the whole thing was php and that the <?php ?> would be good enough in an html file to let it know its php. Thanks!
Was This Post Helpful? 0
  • +
  • -

#8 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: load external html file into div with php

Posted 25 March 2012 - 12:00 PM

You can run an html file with the html extension as a script.
You can create a blank text file called .htaccess
In that file write:
AddType application/x-httpd-php .html

Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3391
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: load external html file into div with php

Posted 26 March 2012 - 03:12 AM

though be aware that this runs all your HTML files through the PHP interpreter, even if they donít contain PHP code and thus create more processing load.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1