php 检查链接是否被点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12205307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php check if link has been clicked
提问by user244228
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
嗨,我是 php 的新手,请帮忙。我正在制作一个网站,它有一个菜单,我需要它,以便如果单击“link1”之类的链接,page1.php 将加载到 mainSection div 中,如果单击 link2,page2.php 将加载到 mainSection 等中。所有页面:page1、page2、page3 等将根据点击的链接加载到该单个页面中。这可能吗我不知道从哪里开始。谢谢
<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>
回答by Majid Laissi
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>
回答by mendez7
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.
除了 majid 的代码之外,您还必须检查链接是否已设置,否则会引发未定义的 $link 错误。
- link 1
- link 2
- link 3
- link 4
- 链接 1
- 链接 2
- 链接 3
- 链接 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>
回答by j08691
Change the format of your links to:
将链接的格式更改为:
<a href="/?1" name="link1">link 1</a>...
and then change your PHP to:
然后将您的 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';
}
?>

