[英]How to pass variables between 2 PHP files?
I have the following code in a file called one.php 我在名为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>
Now when I click on one of the anchor tag links, it takes me to two.php. 现在,当我点击其中一个锚标记链接时,我需要两个.php。
Here I want the output to be, 在这里,我想要输出,
One was clicked.
OR Two was clicked.
Two was clicked.
或Two was clicked.
How do I make the above output in two.php using PHP? 如何使用PHP在two.php中进行上述输出?
The simplest solution is to append a paramter to the url 最简单的解决方案是在网址上附加一个参数
<a href="two.php?clicked=1">Click here for 1.</a>
<a href="two.php?clicked=2">Click here for 2.</a>
then in PHP 然后在PHP中
if (isset($_GET['clicked'])) {
$clicked = (int)$_GET['clicked'];
} else {
$clicked = 0;
}
code is here, 代码在这里,
<?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>
You can pass variables by using GET
or POST
method. 您可以使用GET
或POST
方法传递变量。
Here is simple GET (query string)
method. 这是简单的GET (query string)
方法。
main.php : 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 : two.php:
<?php
if(!empty($_GET['btn'])) {
echo $_GET['btn'] . " was clicked";
}
?>
This question is asked so many times:- 这个问题被问了很多次: -
How to pass a variable value between 2 php files? 如何在2个php文件之间传递变量值?
How to pass variables between php scripts? 如何在PHP脚本之间传递变量?
Pass variables between two PHP pages without using a form or the URL of page 在不使用表单或页面URL的情况下在两个PHP页面之间传递变量
passing value between two php files 在两个php文件之间传递值
PHP Pass variable to next page PHP Pass变量到下一页
Passing PHP variables between scripts 在脚本之间传递PHP变量
passing variables between functions and files in php 在php中传递函数和文件之间的变量
PHP Passing variables between two files PHP在两个文件之间传递变量
Passing multiple variables to another page in url 将多个变量传递到url中的另一个页面
Use SESSIONS variables between different php files 在不同的php文件之间使用SESSIONS变量
how to pass variables between 2 php pages when the latter called with require() 当后者用require()调用时,如何在2个php页面之间传递变量
Transfer variables between PHP pages phpbb 3.1 passing variable between 2 pages 在PHP页面 之间传递变量phpbb 3.1在2页之间传递变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.