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 >
<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 >
<h3 >Always Popular</h3>
<div >
<!-- first product -->
<div >
<div >
<img src="images/white-tee-1.jpg" alt="Trendy, over-designed white T-shirt">
<button >Add to cart</button>
</div>
<div >
<h4 >Slightly<span style="color: #f28816"> White T-Shirt</span></h4>
<span >£566.99</span>
</div>
</div>
<!-- second product-->
<div >
<div >
<img src="images/white-tee-2.jpg" alt="Common Mans White T-Shirt">
<button >Add to cart</button>
</div>
<div >
<h4 >Very<span style="color: #f28816"> White T-Shirt</span></h4>
<span >£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 >
<link rel="stylesheet" href="../css/nav.css">
<div >
<h1 > T-<span style="color: #f28816">Drip</span><br>Online<span style="color: #f28816"> Store</span></h1>
<div >
<a href="../cart.php"><img src="../images/cart.png" alt="cart"></a>
</div>
</div>
<ul >
<li ><a href="../index.php" >home</a></li>
<li ><a href="../about.php" >about</a></li>
<li ><a href="../store.php" >store</a></li>
</ul>
</nav>
Also a screenshot of the folder holding all the files just in case :
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.
CodePudding user response:
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.
CodePudding user response:
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
andrequire
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.