Search code examples
phphtmlinclude

PHP NavBar works on its own but not when included in other PHP code


I'm dealing with small problem in my PHP code, when I try to use include on my mock website, to have the same navbar on all of my pages, it simply doesn't work, however it does work when I open it on its own.

I'm very new to coding so please bare with me and my complete lack of proper terminology.

This is my index.php :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>T-drip Home</title>
    <link rel="stylesheet" href="css/home.css">
    <?php include('inc/nav.php') ?>
</head>
<body>
    <div class="fadeIn">
        <img src="images/home-white-tee-1.jpg" alt="Fancy 1st Tee">
        <img src="images/home-white-tee-2.jpg" alt="Provocative 2nd Tee">
        <img src="images/home-white-tee-3.jpg" alt="Classy 3rd Tee">
    </div>
    <section class="prod">
        <h3 class="prodCat">Always Popular</h3>
        <div class="prodCont">
        <!-- first product -->
            <div class="prodCard">
                <div class="prodJpg">
                    <img src="images/white-tee-1.jpg" class="prodTh" alt="Trendy, over-designed white T-shirt">
                    <button class="jpgBtn">Add to cart</button>
                </div>
                <div class="prodInfo">
                <h4 class="prodName">Slightly<span style="color: #f28816"> White T-Shirt</span></h4>
                <span class="price">£566.99</span>
                </div>
            </div>
            <!-- second product-->
            <div class="prodCard">
                <div class="prodJpg">
                    <img src="images/white-tee-2.jpg" class="prodTh" alt="Common Mans White T-Shirt">
                    <button class="jpgBtn">Add to cart</button>
                </div>
                <div class="prodInfo">
                <h4 class="prodName">Very<span style="color: #f28816"> White T-Shirt</span></h4>
                <span class="price">£364.99</span>
                </div>
            </div>
        </div>
    </section>
    <!-- js is needed for the aforementioned fade gallery -->
    <script type="text/javascript" src="inc/script.js">
    </script>

</body>
</html>

Index.php works as it should with an exception of the <?php include('inc/nav.php') ?> line which just doesn't work at all, as if that code was not there to begin with.

This is my nav.php :

<nav class="navBar">
    <link rel="stylesheet" href="../css/nav.css">
    <div class="nav">
        <h1 class="logo"> T-<span style="color: #f28816">Drip</span><br>Online<span style="color: #f28816"> Store</span></h1>
        <div class="profBas">
            <a href="../cart.php"><img src="../images/cart.png" alt="cart"></a>
        </div>
    </div>
    <ul class="linkCont">
        <li class="linkItm"><a href="../index.php" class="link">home</a></li>
        <li class="linkItm"><a href="../about.php" class="link">about</a></li>
        <li class="linkItm"><a href="../store.php" class="link">store</a></li>
    </ul>
</nav>

Also a screenshot of the folder holding all the files just in case :

ss of the folder

I've been pulling my hair on this problem for quite some time now and I would greatly appreciate your help in this matter.

Edit 1:

Those files are not on any server currently, I'm just viewing them using firefox browser with web developer extension on it. I should have said so sooner, sorry for that.

I've moved include line to the <body> as suggested but that didn't help. (I'll leave it there for all other suggested fixes since it's where it should have been to begin with).

I've also tried:

ini_set( 'error_reporting', E_ALL );
ini_set( 'display_errors', true );

include './members/reg.php';

and

<?php
ini_set( 'error_reporting', E_ALL );
ini_set( 'display_errors', true );

include './members/reg.php';
?>

but neither of those helped me.

Also to clarify a bit, nothing changes when I view the file regardless if I have the include line in the code or not.


Okay, I give up, I don't understand why the include is not working properly. I'll just copy paste the nav in every page instead, thank you for your help though, I appreciate it.


Solution

    • As correctly stated in @Ramkrishna's answer the HTML <nav> element should be inside the HTML <body> tag. Not the <head>.

    • As for not showing the nav.php file; you need to check that your filepath is correct. Does directory_containing_PHP_HTMLfile/inc/nav.php exist? Is it readable?

    • It is best practise to give an absolute filepath for the include, from the Server Document Root directory (typically the base directory of the web-accessible part of your server)

    NOTE: $_SERVER['DOCUMENT_ROOT'] will not work when running only on the local(host) machine rather than a server machine. Use an alternative.

    • Also, include and require do NOT need the brackets.

    All in all this brings us to:

    <html>
        <head>...</head>
        <body>
            <?php 
            include $_SERVER['DOCUMENT_ROOT']."/inc/nav.php";
            ?>
        </body>
    </html>
    

    Finally You should Read your PHP Error logs which will tell you if there is a problem with the included file.