如何在2个PHP文件之间传递变量?

时间:2016-03-09 05:41:11

标签: php html dom

我在名为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中创建上述输出?

4 个答案:

答案 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)

您可以使用GETPOST方法传递变量。

这是简单的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)