How to include a file from another directory in index.php

Asked

Viewed 180 times

1

I have in my root folder my index php.. I also have, in my root folder, another folder called components.

Inside the folder components, I have a file called php menu., containing the site menu codes.

I want to include the menu file in my index. When I do this - using the code below - it opens, but with the wrong links.

<?php include "componentes/menu.php";?>

For example:

In the php menu. is

<ul>
   <a href="../index.php"> <li>home<li> </a>
<ul>

The link above will open normally, but if I "pull" the file php menu. in another directory, will give error because of the ../index.php

1 answer

-2


Friend, first of all, you should use Javascript... Follow the example:

<!DOCTYPE html>
<html>
<head>
    <title>Teste</title>
    <!-- importe a biblioteca jquery para usar o código javascript-->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
        <div id="menu"></div>

        <script type="text/javascript">
            $("#menu").load("componentes/menu.html");
        </script>
</body>
</html>

Menu.html file in component folder:

<ul>
    <a href="./pagina.php"><li>Teste</li></a>
</ul>
  • Vlw I’ll do a little research on that, we’ll do it together!

  • Not at all, friend ><

  • consequi do, the more the link problem continues! if I create another folder at the root and create a file Nala and pull up the menu.php, will give error link

  • in the start menu I put <a href=". /index.php"> when I pulled on index.php it was perfect! more if I create another folder at the root, and create a file Nala, and pull up the menu.php the menu directory changes relative to that new folder!

  • Friend, it is this way because the menu should be pulled only in the files of the site root. Unless you leave the menu file inside the main root instead of the component folder. Try this

  • got it! what you’re not able to do! I’ll leave the menu next to the index thanks for everything! S2

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.