简体   繁体   English

如何使用 URL 传递 PHP 变量

[英]How to pass a PHP variable using the URL

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".单击链接Link1Link2 ,我得到“单击链接 2”。 Why?为什么?

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.另一种方法是首先将变量初始化为您对 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.而且,更好的方法是将$a$b变量包含在单个 PHP 文件中,然后将其包含在您将使用这些变量的所有页面中,正如 Tim Cooper 在他的帖子中提到的那样。 You can also include this in a session.您也可以将其包含在会话中。

You're passing link=$a and link=$b in the hrefs for A and B, respectively.您分别在 A 和 B 的 href 中传递link=$alink=$b They are treated as strings, not variables.它们被视为字符串,而不是变量。 The following should fix that for you:以下应该为您解决这个问题:

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 . $a的值也不包含在pass.php I would suggest making a common variable file and include it on all necessary pages.我建议制作一个通用变量文件并将其包含在所有必要的页面上。

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)

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;
?>

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>";

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>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM