2

I created a PHP file (static_designs.php) which contains codes for the header. This php calls a javascript file (sidebar-open-close.js) using <script src="sidebar_open-close.js"></script>. Now I have a page (mainpage.php) that includes static_designs.php using include 'static_designs/static_designs.php';

Note that in the static_designs directory is where my javascript and php.

This is the Directory Levels:

  • /mainpage.php
  • /static_designs/static_designs.php
  • /static_designs/sidebar_open-close.js

Here are the codes:

static_designs.php

 <div class="w3-display-left custom-left-item ">
                <button class="w3-button w3-white w3-large" onclick="w3_open()">☰</button>
 </div>

<div class="w3-sidebar w3-bar-block w3-border-right w3-animate-left" style="display:none" id="mySidebar">
    <button onclick="w3_close()" class="w3-bar-item w3-large">Close &times;</button>
    <a href="#" class="w3-bar-item w3-button">Home</a>
    <a href="#" class="w3-bar-item w3-button">Services</a>
    <a href="#" class="w3-bar-item w3-button">Packages</a>

</div>

sidebar_open-close.js

 function w3_open() {
    document.getElementById("mySidebar").style.display = "block";
}
function w3_close() {
    document.getElementById("mySidebar").style.display = "none";
}

mainpage.php

<?php
include 'static_designs/static_designs.php';
include 'static_designs/footer.php';

?>

Note: Footer is not a problem here.

When I try to run static_designs.php, the sidebar works (closes and opens), but when I run mainpage.php, the format/css looks the same, but the javascript doesn't seem to work (pressing the menu button doesn't do anything).

The reason why I want to choose the method "include" php files is that the header and the footer will be the same for many pages. So, my question is, is there a way to resolve this or a different method that is more recommended to include a header and a footer without having to copy paste it to every page?

Zirc
  • 460
  • 3
  • 14
  • `include '/static_designs/static_designs.php';` try that –  Dec 18 '18 at 19:00
  • use full url for the script src like http://www.yoursite/static_designs/sidebar_open-close.js ...or... always use the folder like src= "static_designs/sidebar_open-close.js" this will not work in sidebar directly but will work on homepage or any page. – Nawed Khan Dec 18 '18 at 19:01
  • okay. I'll try that, but I'm pretty sure putting a / before only indicates a backtrack on directory level. see https://stackoverflow.com/questions/5559578/having-links-relative-to-root – Zirc Dec 18 '18 at 19:02
  • Good idea @NawedKhan, I will surely take that into consideration, but dealing with url seems harder when being tested on local machines. – Zirc Dec 18 '18 at 19:04

0 Answers0