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>
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>
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.
<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>
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';
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With