【问题标题】:How to pass a PHP variable using the URL如何使用 URL 传递 PHP 变量
【发布时间】:2011-03-26 03:29:56
【问题描述】:

我想使用 URL 传递一些 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>

单击链接 Link1Link2 时,我得到“链接 2 已单击”。为什么?

【问题讨论】:

  • 看看pass.php页面上的url,有什么发现吗??
  • 是的,变量丢失(未显示在 url 中)....修复了问题
  • 你可以这样做:``` ```跨度>

标签: php url


【解决方案1】:

在您的 link.php 中,您的 echo 语句必须是这样的:

echo '<a href="pass.php?link=' . $a . '>Link 1</a>';
echo '<a href="pass.php?link=' . $b . '">Link 2</a>';

那么在你的 pass.php 中你不能使用 $a 因为它没有用你想要的字符串值初始化。

您可以直接将其与这样的字符串进行比较:

if($_GET['link'] == 'Link1')

另一种方法是首先将变量初始化为您对link.php 所做的相同操作。而且,更好的方法是将$a$b 变量包含在一个PHP 文件中,然后将其包含在您将使用这些变量的所有页面中,正如Tim Cooper 在他的帖子中提到的那样。您也可以将其包含在会话中。

【讨论】:

    【解决方案2】:

    您在 A 和 B 的 href 中分别传递了 link=$alink=$b。它们被视为字符串,而不是变量。以下应该为您解决这个问题:

    echo '<a href="pass.php?link=' . $a . '">Link 1</a>';
    
    // and
    
    echo '<a href="pass.php?link=' . $b . '">Link 2</a>';
    

    $a 的值也不包含在pass.php 中。我建议制作一个通用变量文件并将其包含在所有必要的页面上。

    【讨论】:

      【解决方案3】:

      以上所有答案都是正确的,但我注意到一些非常重要的事情。在变量和等号之间留一个空格可能会导致问题。例如(?variablename =value)

      【讨论】:

        【解决方案4】:

        我在 Skytopia 的“Super useful bits of PHP, Form and JavaScript code”中找到了这个解决方案。

        在“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>
        

        “page2c.php”内:

        <?php
            // Retrieve the URL variables (using PHP).
            $num = $_GET['myNumber'];
            $fruit = $_GET['myFruit'];
            echo "Number: ".$num."  Fruit: ".$fruit;
        ?>
        

        【讨论】:

        • 你应该考虑给参考做广告
        • 您能解释一下这是如何工作的吗? myNumber={$row[0]} 我无法检索此值
        • 见“How to reference material written by others”。对从其他网站借来的信息给予适当的归属对 SO 非常重要。
        【解决方案5】:

        使用这个简单的方法

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

        【讨论】:

          【解决方案6】:

          你可以这样做:

          <a href="./gemsPack.php?uid=<?php echo $id; ?>&amount=1.99">
          
          

          【讨论】:

            【解决方案7】:

            随便放

            $a='Link1';
            $b='Link2';
            

            在您的 pass.php 中,您将得到答案并在您的 link.php 中添加双引号:

            echo '<a href="pass.php?link=' . $a . '">Link 1</a>';
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-09-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多