1

I've just begun developing and I have a working page with a header and body, but I want to start splitting up all of the code by type. I have successfully pulled out the css and javascript but I haven't been able to pull out my header. Right now my files look like this:

index.php:

<!DOCTYPE html>
<html>
  <head>
     Some Stuff Here
     <link rel="stylesheet" href="style1.css">
  </head>
  <body>
     <?php include("header.html");?>
     Some Stuff Here
  </body>
</html>

header.html:

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="style2.css">
  </head>
  <body>
    <script src="header.js"></script>   
    Some Stuff Here
  </body>
</html>

When I load index.php in my browser I get the body and its styling, but the header is absent. What's up with me here?

/* EDIT */

Interestingly I have found that when I load the page from my Synology DS713+ NAS (on which MySQL and phpMyAdmin are installed) the page loads the header just fine, but on my thinkpad (on which I use XAMPP) the page does not add the header. This solves the problem for me since my production environment is my NAS, but perhaps someone can provide an answer for why this might happen.

4
  • Your header.html shouldn't include the full html (ie html head body), it should just contain the html you want to put in that part. Commented May 8, 2015 at 20:33
  • how then can I include the javascript and css associated with the header? Commented May 8, 2015 at 20:35
  • Just put the script/link tags with the html, they dont have to go in the head element. Commented May 8, 2015 at 20:38
  • This isn't working for me. I did not change index.php and header.html now includes only the script/link tags and what used to be between the body tags. Commented May 8, 2015 at 20:48

2 Answers 2

5

Your resulting HTML would look something like this:

<!DOCTYPE html>
<html>
  <head>
     Some Stuff Here
     <link rel="stylesheet" href="style1.css">
  </head>
  <body>
     <!DOCTYPE html>
     <html>
       <head>
         <link rel="stylesheet" href="style2.css">
       </head>
       <body>
         <script src="header.js"></script>   
         Some Stuff Here
       </body>
     </html>
     Some Stuff Here
  </body>
</html>

That won't render how you want it. You will want to do the following.

PHP - index.php

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="style1.css">
    <link rel="stylesheet" href="style2.css">
  </head>
  <body>
    <?php include("header.html");?>  
    Some Stuff Here
  </body>
</html>

HTML - header.html

<script src="header.js"></script>   
Some Stuff Here
Sign up to request clarification or add additional context in comments.

1 Comment

What are you seeing when you review the Source Code?
1

Don't think PHP includes a .html file. Please rename header.html to header.php

Here index.php

<!DOCTYPE html>
<html>
  <head>
    Some Stuff Here
     <link rel="stylesheet" href="style1.css">
  </head>
  <body>
     <?php include("header.php");?>
     Some Stuff Here
  </body>
</html>

And header.php

<link rel="stylesheet" href="style2.css">
<script src="header.js"></script>   
Some Stuff Here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.