0

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.

Robot Dog
  • 3
  • 3
  • Is this a local website on your machine or is it setup at a hosting provider? The reason I ask is that you could be receiving an error but they're suppressed in production and so you're not seeing it. If it's not fatal which I think a missing include is a ***Warning*** then you won't see it. Also, see the answer below. It could be that the include is working fine and the code is being stripped out for improper placement of it in the `` tag. Try moving it to the `` tag. – War10ck Aug 10 '22 at 16:56
  • Does this answer your question? [php include doesn't work even inside the same folder](https://stackoverflow.com/questions/14051449/php-include-doesnt-work-even-inside-the-same-folder) – Martin Aug 10 '22 at 17:05
  • See also [This question and answer](https://stackoverflow.com/questions/10064770/php-include-doesnt-work) – Martin Aug 10 '22 at 17:06
  • 1
    @War10ck the error will still be in the logs, however. – Martin Aug 10 '22 at 17:07
  • 1
    @Martin True. Good place to look as well. – War10ck Aug 10 '22 at 18:04

2 Answers2

1

The first reason could be that the navbar in the code is inside the <head> tag which should be outside the <body> tag. do this first then we find out next reason.

  • Thank you for your message, I've moved `````` to the `````` tag but that didn't change anything, I've also tried to put it between head and body but that didn't do anything either – Robot Dog Aug 10 '22 at 17:05
  • 1
    @RobotDog nothing should ever be *between* `` and `` – Martin Aug 10 '22 at 17:10
  • Can you tell what is the problem in Navbar and what you want to show is not clear? – Ramkrishna Sahu Aug 10 '22 at 17:16
  • Well not only the navbar doesn't show but it appears like nothing is changing in the appearance of the site whether I have the ```include``` line in the code or not. – Robot Dog Aug 10 '22 at 18:03
  • Oh and the code from nav.php works perfectly well whenever I replace ```include``` line with it in index.php (with small changes to the filepaths of course) – Robot Dog Aug 10 '22 at 18:24
0
  • 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.

Martin
  • 22,212
  • 11
  • 70
  • 132
  • Thank you for your response @Martin, I've tried to implement suggested code but it didn't help sadly. I'm looking into Error logs question right now but it might take me some time since I'm not to familiar yet with the terminology used. – Robot Dog Aug 10 '22 at 17:59
  • @RobotDog sorry, I realise that `$_SERVER['DOCUMENT_ROOT']` will not always work when running only on the local machine rather than a server machine. Suggest you remove that variable from the code. – Martin Aug 10 '22 at 18:37