Check php if link was clicked

Hi, I am new to php, can anyone help. I am creating a website, I have a menu, I need it, so if I click the link1 link, page1.php will be loaded into the mainSection div, and if link2 is clicked, then page2.php will be loaded into mainSection, etc. etc., therefore, all pages: page1, page2, page3, etc. will load on this page depending on which link was clicked. Is this possible? I don’t know where to start. Thanks

    <body>
        <?php
            <ul>
                <li><a href="#" name="link1">link 1</a></li>
                <li><a href="#" name="link2">link 2</a></li>
                <li><a href="#" name="link3">link 3</a></li>
                <li><a href="#" name="link4">link 4</a></li>    
            </ul>
        ?>

       <div id="mainSection">
            <?php
        if (link1 == true){
             include 'page1.php';
        }
        if (link2 == true){
            include 'page2.php';
        }
        if (link3 == true){
            include 'page3.php';
        }
        if (link4 == true){
            include 'page4.php';
        }
            ?>  
        </div>
    </body>
+5
source share
3 answers

In addition to the majid code, you need to check if the link has been set, or if it causes an undefined $ link error.
                   

  •               
  • reference 1              
  • reference 2              
  • reference 3              
  • reference 4
              
   <div id="mainSection">
        <?php
    if(isset($_GET['link'])){
    $link=$_GET['link'];
    if ($link == '1'){
         include 'page1.php';
    }
    if ($link == '2'){
        include 'page2.php';
    }
    if ($link == '3'){
        include 'page3.php';
    }
    if ($link == '4'){
        include 'page4.php';
    }
      }  ?>  
    </div>
</body>
+2

<body>
            <ul>
                <li><a href="?link=1" name="link1">link 1</a></li>
                <li><a href="?link=2" name="link2">link 2</a></li>
                <li><a href="?link=3" name="link3">link 3</a></li>
                <li><a href="?link=4" name="link4">link 4</a></li>    
            </ul>

       <div id="mainSection">
            <?php
        $link=$_GET['link'];
        if ($link == '1'){
             include 'page1.php';
        }
        if ($link == '2'){
            include 'page2.php';
        }
        if ($link == '3'){
            include 'page3.php';
        }
        if ($link == '4'){
            include 'page4.php';
        }
            ?>  
        </div>
    </body>
+1

:

<a href="/?1" name="link1">link 1</a>...

PHP :

<?php
        if ($_SERVER['QUERY_STRING'] == 1){
             include 'page1.php';
        }
        if ($_SERVER['QUERY_STRING'] == 2){
            include 'page2.php';
        }
        if ($_SERVER['QUERY_STRING'] == 3){
            include 'page3.php';
        }
        if ($_SERVER['QUERY_STRING'] == 4){
            include 'page4.php';
        }
?>  
0

All Articles