如何使用 URL 传递 PHP 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5440197/
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
How to pass a PHP variable using the URL
提问by chetan
I want to pass some PHP variables using the URL.
我想使用 URL 传递一些 PHP 变量。
I tried the following code:
我尝试了以下代码:
link.php
链接.php
<html>
<body>
<?php
$a='Link1';
$b='Link2';
echo '<a href="pass.php?link=$a">Link 1</a>';
echo '<br/>';
echo '<a href="pass.php?link=$b">Link 2</a>';
?></body></html>`</pre></code>
pass.php
<pre><code>`<html>
<body>
<?php
if ($_GET['link']==$a)
{
echo "Link 1 Clicked";
} else {
echo "Link 2 Clicked";
}
?></body></html>
Upon clicking the links Link1
and Link2
, I get "Link 2 clicked". Why?
单击链接Link1
和 后Link2
,我得到“单击了链接2”。为什么?
回答by ace
In your link.php your echo
statement must be like this:
在您的 link.php 中,您的echo
声明必须是这样的:
echo '<a href="pass.php?link=' . $a . '>Link 1</a>';
echo '<a href="pass.php?link=' . $b . '">Link 2</a>';
Then in your pass.php you cannot use $a
because it was not initialized with your intended string value.
然后在你的 pass.php 中你不能使用,$a
因为它没有用你想要的字符串值初始化。
You can directly compare it to a string like this:
您可以直接将其与这样的字符串进行比较:
if($_GET['link'] == 'Link1')
Another way is to initialize the variable first to the same thing you did with link.php. And, a much better way is to include the $a
and $b
variables in a single PHP file, then include that in all pages where you are going to use those variables as Tim Cooper mention on his post. You can also include this in a session.
另一种方法是首先将变量初始化为您对 link.php 所做的相同的事情。而且,更好的方法是将$a
和$b
变量包含在单个 PHP 文件中,然后将其包含在您将使用这些变量的所有页面中,正如 Tim Cooper 在他的帖子中提到的那样。您也可以将其包含在会话中。
回答by Tim Cooper
You're passing link=$a
and link=$b
in the hrefs for A and B, respectively. They are treated as strings, not variables. The following should fix that for you:
你分别传入link=$a
和link=$b
传入A 和B 的href。它们被视为字符串,而不是变量。以下应该为您解决这个问题:
echo '<a href="pass.php?link=' . $a . '">Link 1</a>';
// and
echo '<a href="pass.php?link=' . $b . '">Link 2</a>';
The value of $a
also isn't included on pass.php
. I would suggest making a common variable file and include it on all necessary pages.
的值$a
也不包含在 中pass.php
。我建议制作一个通用变量文件并将其包含在所有必要的页面上。
回答by Hassan Musa dabra
All the above answers are correct, but I noticed something very important. Leaving a space between the variable and the equal sign might result in a problem. For example, (?variablename =value)
以上所有答案都是正确的,但我注意到一些非常重要的事情。在变量和等号之间留一个空格可能会导致问题。例如,(?variablename =value)
回答by jonah
Use this easy method
使用这个简单的方法
$a='Link1';
$b='Link2';
echo "<a href=\"pass.php?link=$a\">Link 1</a>";
echo '<br/>';
echo "<a href=\"pass.php?link=$b\">Link 2</a>";
回答by Devarsh Bhatt
just put
就放
$a='Link1';
$b='Link2';
in your pass.php and you will get your answer and do a double quotation in your link.php:
在你的 pass.php 中,你会得到你的答案并在你的 link.php 中做一个双引号:
echo '<a href="pass.php?link=' . $a . '">Link 1</a>';
回答by palyarmerc
I found this solution in "Super useful bits of PHP, Form and JavaScript code" at Skytopia.
我在 Skytopia 的“ PHP、表单和 JavaScript 代码的超级有用部分”中找到了这个解决方案。
Inside "page1.php" or "page1.html":
在“page1.php”或“page1.html”中:
// Send the variables myNumber=1 and myFruit="orange" to the new PHP page...
<a href="page2c.php?myNumber=1&myFruit=orange">Send variables via URL!</a>
//or as I needed it.
<a href='page2c.php?myNumber={$row[0]}&myFruit={$row[1]}'>Send variables</a>
Inside "page2c.php":
在“page2c.php”里面:
<?php
// Retrieve the URL variables (using PHP).
$num = $_GET['myNumber'];
$fruit = $_GET['myFruit'];
echo "Number: ".$num." Fruit: ".$fruit;
?>