0

Sorry if I sound like an idiot, but I'm relatively new to php.

Basically, I want to have a navbar that is synced across all the pages of my website.

What I have so far is this:

navbar.php

<?php
echo '<link rel="stylesheet" type="text/css" href="style.css"> -
<ul> -
<li><a href="index.html"><div id="nav"><span id="middle">Home</span></div></a> -
<li><a href="art.html"><div id="nav"><span id="middle">Art</span></div></a></li> -
<li><a href="games.html"><div id="nav"><span id="middle">Games</span></div></a></li> -
<li><a href="wish.html"><div id="nav"><span id="middle">Wish List</span></div></a></li> -
<li><a href="dw.html"><div id="nav"><span id="middle">Doctor Who</span></div></a></li> -
</ul>'; 
?>

index.html

<!DOCTYPE html>
<html>

<head>
  <title>Colin Site</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <link rel="stylesheet" type="text/css" href="style.css">
  <style>
    p {
      font-size: 150%;
    }
  </style>
</head>

<body>
  <div class="navcontain">
    <?php include "nav.php"; ?>
  </div>
  <br>
  <br>
  <br>
  <h2>Welcome to my website, where I do random things</h2>
</body>

</html>

style.css

#navbar {
  width: 100%;
}
#nav {
  background-color: #848482;
  width: 110px;
  height: 40px;
  text-align: center;
  font-size: 100%;
  color: white;
}
#navcontain {
  position: relative;
  top: 1%;
  background-color: alpha;
  width: 100%;
}

When I go to my website, there's just a blank space where my navbar should be.

Thanks in advance

BRFNGRNBWS
  • 13
  • 3

3 Answers3

3

You can't execute PHP code in a HTML file. Look at the extension of the file where you're including the navbar.php it's .html right? It's wrong then. In order to make it works rename it to index.php.

ReynierPM
  • 15,161
  • 39
  • 158
  • 314
  • Technically speaking, you're not entirely correct. *By default*, HTML files aren't preprocessed, but that can be changed: http://stackoverflow.com/questions/11312316/how-do-i-add-php-code-file-to-html-html-files – Tieson T. Oct 17 '16 at 18:49
  • 1
    And I believe that's not the way to go because you're sending a simple HTML page through an interpreter just for not to change the extension :-) and therefore all the `.html` pages will go through the same interpreter even if they hasn't a line of PHP code – ReynierPM Oct 17 '16 at 18:51
0

You have <?php include "nav.php"; ?> in your HTML, but according to your question the file is named navbar.php....

(yes, and your main file has to have a php ending, as @ReynierPM wrote)

Johannes
  • 53,485
  • 15
  • 52
  • 104
0

It is not possible using php in a html file, though you can use AJAX / JQuery to load the php into the webpage, heres an axample

index.html

<html>
    <head>
        <script src="jquery.js"></script> <!-- You can get JQuery at http://jquery.com -->
    </head>
    <body>
        <script>
            $(document).ready(function(){
                $.ajaxSetup({cache:false});
                $("#nav").load("nav.php");
            });
        </script>
        <div id="nav"></div>
    </body>
</html>