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.

5
  • 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 <head> tag. Try moving it to the <body> tag. Commented Aug 10, 2022 at 16:56
  • Does this answer your question? php include doesn't work even inside the same folder Commented Aug 10, 2022 at 17:05
  • See also This question and answer Commented Aug 10, 2022 at 17:06
  • 1
    @War10ck the error will still be in the logs, however. Commented Aug 10, 2022 at 17:07
  • 1
    @Martin True. Good place to look as well. Commented Aug 10, 2022 at 18:04

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your message, I've moved <?php include('inc/nav.php') ?> to the <body> 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
@RobotDog nothing should ever be between </head> and <body>
Can you tell what is the problem in Navbar and what you want to show is not clear?
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.
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)
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.

2 Comments

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.
@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.

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.