Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将查询字符串get值传递到url链接_Php_Query String - Fatal编程技术网

Php 将查询字符串get值传递到url链接

Php 将查询字符串get值传递到url链接,php,query-string,Php,Query String,我试图从查询字符串中传入一个$\u GET变量,并将其传递到另一个页面的链接中,该页面上有一个应用程序 客户将被定向到我的页面,url的变量名为merchantid。我需要把它放在主页上,并将其传递到应用程序页面 我把它作为测试显示在主页上,所以我知道如何获取它。我只需要知道如何将其传递到应用程序页面 <?php if (empty($_GET)) { // no data passed by get echo "<a href='{site_

我试图从查询字符串中传入一个$\u GET变量,并将其传递到另一个页面的链接中,该页面上有一个应用程序

客户将被定向到我的页面,url的变量名为merchantid。我需要把它放在主页上,并将其传递到应用程序页面

我把它作为测试显示在主页上,所以我知道如何获取它。我只需要知道如何将其传递到应用程序页面

<?php
    if (empty($_GET)) {
        // no data passed by get
        echo "<a href='{site_url}application'>Application</a>";
    }
    else
    {
        // The value of the variable name is found
        echo "<a href='{site_url}application?merchantid=" .merchantid ."'><Application></a>";
    }
?>

我的else链接现在实际上坏了

好的,这是我的第二次尝试,同样的结果。当我将merchantid传递到url中时,链接就会断开。例如:www.mysite.com/?=merchantid=12345

<?php
    if (empty($_GET)) {
        // no data passed by get
        echo "<a href='{site_url}application'>Application</a>";
    }
    else
    {
        if(isset($_GET['merchantid'])){$merchantid = $_GET['merchantid'];}
        else{$merchantid = "DefaultMerchant";}
            echo "<a href='{$site_url}application?merchantid=" .$merchantid ."'><Application </a>";                                     
    }
?>
为什么您的代码不起作用
您没有告诉php“merchantid”是一个变量,也没有定义它

解决方案 替换

echo "<a href='{site_url}application?merchantid=" .merchantid ."'><Application></a>";
echo”“;

if(isset($\u GET['merchantid']){$merchantid=$\u GET['merchantid'];}
else{$merchantid=”“;}
回声“;
}

更新代码
您需要首先将
$\u GET['merchantid']
分配给
$merchantid
,或者将
$merchantid
替换为
$\u GET['merchantid']
除非您已经打开了register\u globals,而您确实不应该使用它

因此,要么加上:

$merchantid = $_GET['merchantid'];
或者使用以下命令:

echo "<a href='{$site_url}application?merchantid=" . $_GET['merchantid'] . "'><Application></a>";
echo”“;

除此之外,正如其他人所指出的,您的原始代码在变量名之前缺少一个
$

$\u GET是一个数组,由查询字符串中的任何值索引。例如:

http://sit.url.com?merchantId=12&foo=bar
将在$\u GET数组中放置以下内容:

$_GET['merchantId'] = "12"
$_GET['foo'] = "bar"
您需要代码中的一个块根据$\u GET中的值初始化$merchantId变量:

//folks commonly use ternaries for this:
$merchantId = (isset($_GET['merchantId'])) ? $_GET['merchantId'] : false
这是一种表示以下内容的简写方式:

if (isset($_GET['merhantId']) {
  $merchantId = $_GET['merchantId']
} else {
  $merchantId = false;
}

正如Angelo和C.Coggins所提到的,不要忘记php中变量前面的“$”。

merchantid(是$missing?!)从何而来?问题还不清楚-是否使用merchantid作为查询参数调用php并询问如何访问它?PHP是否会产生错误消息?在哪里定义了
$merchantid
?除非您之前已将$GET['merchatid']分配给变量$merchantid,否则应改用$GET['merchantid']。另外,不要忘记isset()检查以避免php通知;如果未定义
$\u GET['merchantid']
,则将不会定义
$merchantid
,因此当您尝试从中读取时将产生警告+1为edit@andrew在以前版本的答案中,如果未设置$\u GET['merchantid'],则不会回显链接。无论如何,我再次更新了我的答案,这将在任何情况下输出链接:)谢谢您的反馈,请参阅我的修订代码。我对如何在内部If语句中显示链接感到困惑。看起来我没有为If语句设置任何内容。您应该检查
isset($\u GET['merchantid'])
,否则您的任一解决方案都可能产生警告true。我只是想指出如何按照OP的要求获取
$\u get['merchantid']
值。
//folks commonly use ternaries for this:
$merchantId = (isset($_GET['merchantId'])) ? $_GET['merchantId'] : false
if (isset($_GET['merhantId']) {
  $merchantId = $_GET['merchantId']
} else {
  $merchantId = false;
}