php 通过 URL 传递多个变量并在下一页读取所有变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9832508/
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
Pass multiple variables via URL and reading all of them on next page
提问by Danny
I have a link that points to a webpage e.g. "land.php". The link looks like this:
我有一个指向网页的链接,例如“land.php”。该链接如下所示:
<a href="land.php?id=1&cd=a">link</a>
this takes me to the page land.php where I can read the first parametere with $id
(and it is equal to 1, correctly), but I cannot read the second one. I either tried with $cd
or $_GET['cd']
. None of them works.
这将我带到land.php 页面,我可以在其中读取第一个参数$id
(并且它正确地等于1),但我无法读取第二个参数。我要么尝试使用$cd
要么$_GET['cd']
。它们都不起作用。
if I tried isset($cd)
it says false
. Same thing for isset($_GET['cd'])
.
如果我试过isset($cd)
它会说false
。同样的事情isset($_GET['cd'])
。
How can I pass the second parameter too (and read it!)?
我怎样才能传递第二个参数(并阅读它!)?
EDIT:
编辑:
some code (so people are happy. I think it's pointless in this case..).
一些代码(所以人们很高兴。我认为在这种情况下它毫无意义......)。
land.php
土地.php
<?php
if($_GET['cd']==a)
echo "<h2>HI</h2>";
else
echo "<h2>BY</h2>";
?>
if I use $cd
instead of $_GET['cd']
it doesn't work anyway..
如果我使用$cd
而不是$_GET['cd']
它无论如何都不起作用..
EDIT2 I don't get any syntax error, it just doesn't behave how expected.
EDIT2 我没有收到任何语法错误,它只是不符合预期。
回答by Ragnar123
The value is stored in $_GET['cd']
.
该值存储在$_GET['cd']
.
Try printing out the $_GET
array, with print_r($_GET);
尝试打印出$_GET
数组,使用print_r($_GET);
print_r($_GET)should output
print_r($_GET)应该输出
Array
(
[id] => 1
[cd] => a
)
This should ofcourse be in the land.php
page, as the get variables are only available in the requested page.
这当然应该在land.php
页面中,因为获取变量仅在请求的页面中可用。
回答by Andreas Hagen
Your server might be set up to accept semicolon instead of ampersands. Try replacing & with ;
您的服务器可能设置为接受分号而不是&符号。尝试用 ; 替换 &
回答by Phil
$_GET['cd']
is the correct syntax. Are you actually on the land.php
page, ie does your browser's address bar read something like
$_GET['cd']
是正确的语法。您是否真的在land.php
页面上,即您浏览器的地址栏是否显示类似
example.com/land.php?id=1&cd=a
Also, it looks like you have register_globals
enabled if you can read $id
. This is a very bad idea.
此外,register_globals
如果您可以阅读$id
. 这是一个非常糟糕的主意。
Update
更新
Your code snippet contains syntax errors. I recommend the following, including enabling decent error reporting for development
您的代码段包含语法错误。我推荐以下内容,包括为开发启用适当的错误报告
ini_set('display_errors', 'On');
error_reporting(E_ALL);
if(isset($_GET['cd']) && $_GET['cd'] == 'a') {
echo "<h2>HI</h2>";
} else {
echo "<h2>BY</h2>";
}