Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php check if link has been clicked

Tags:

php

Hi I'm a novice at php could some please help. I'm making a website it has a menu, I need it so that if a link like "link1" is clicked page1.php will load into the the mainSection div and if link2 is clicked page2.php will load in mainSection etc. so all the pages: page1, page2, page3 etc will load into this single page depending on what link has been 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>
like image 772
user244228 Avatar asked Aug 30 '12 20:08

user244228


3 Answers

Here's something you can start with

<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>
like image 116
Majid Laissi Avatar answered Nov 14 '22 11:11

Majid Laissi


In addition to majid's code you have to check if the link has been set or else it throws an error of undefined $link.

  • link 1
  • link 2
  • link 3
  • link 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>
like image 44
mendez7 Avatar answered Nov 14 '22 10:11

mendez7


Change the format of your links to:

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

and then change your PHP to:

<?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';
        }
?>  
like image 1
j08691 Avatar answered Nov 14 '22 11:11

j08691