我在名为one.php的文件中有以下代码
<?php
session_start();
$_SESSION['one'] = "ONE";
$_SESSION['two'] = "TWO";
?>
<html>
<body>
<a href="two.php">Click here for 1.</a>
<a href="two.php">Click here for 2.</a>
</body>
</html>
现在,当我点击其中一个锚标记链接时,我需要两个.php。
这里我希望输出为
One was clicked.
或Two was clicked.
如何使用PHP在two.php中创建上述输出?
答案 0 :(得分:2)
最简单的解决方案是将一个参数附加到网址
<a href="two.php?clicked=1">Click here for 1.</a>
<a href="two.php?clicked=2">Click here for 2.</a>
然后在PHP中
if (isset($_GET['clicked'])) {
$clicked = (int)$_GET['clicked'];
} else {
$clicked = 0;
}
答案 1 :(得分:1)
代码就在这里,
<?php
if($_REQUEST['click'] == 1)
{
echo "One was clicked.";
}
else if($_REQUEST['click'] == 2)
{
echo "Two was clicked.";
}
?>
<html>
<body>
<a href="two.php?click=1">Click here for 1.</a>
<a href="two.php?click=2">Click here for 2.</a>
</body>
</html>
答案 2 :(得分:0)
这是简单的GET (query string)
方法。
main.php:
<html>
<body>
<a href="two.php?btn=One">Click here for 1.</a>
<a href="two.php?btn=Two">Click here for 2.</a>
</body>
</html>
two.php:
<?php
if(!empty($_GET['btn'])) {
echo $_GET['btn'] . " was clicked";
}
?>
答案 3 :(得分:0)
这个问题被问过很多次: -
How to pass a variable value between 2 php files?
How to pass variables between php scripts?
Pass variables between two PHP pages without using a form or the URL of page
passing value between two php files
PHP Pass variable to next page
Passing PHP variables between scripts
passing variables between functions and files in php
PHP Passing variables between two files
Passing multiple variables to another page in url
Use SESSIONS variables between different php files
how to pass variables between 2 php pages when the latter called with require()
Transfer variables between PHP pages phpbb 3.1 passing variable between 2 pages