php 如何设置 $_GET 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5415224/
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 set $_GET variable
提问by dave
How do i set the variable that the $_GET
function will be able to use, w/o submitting a form with action = GET
?
我如何设置$_GET
函数将能够使用的变量,而无需提交表单action = GET
?
采纳答案by Gaurav
You can create a link , having get variable in href.
您可以创建一个链接,在href 中有get 变量。
<a href="www.site.com/hello?getVar=value" >...</a>
回答by Pascal MARTIN
$_GET
contains the keys / values that are passed to your script in the URL.
$_GET
包含传递给 URL 中脚本的键/值。
If you have the following URL :
如果您有以下网址:
http://www.example.com/test.php?a=10&b=plop
Then $_GET
will contain :
然后$_GET
将包含:
array
'a' => string '10' (length=2)
'b' => string 'plop' (length=4)
Of course, as $_GET
is not read-only, you could also set some values from your PHP code, if needed :
当然,由于$_GET
不是只读的,如果需要,您也可以从 PHP 代码中设置一些值:
$_GET['my_value'] = 'test';
But this doesn't seem like good practice, as $_GET
is supposed to contain data from the URL requested by the client.
但这似乎不是好的做法,因为$_GET
它应该包含来自客户端请求的 URL 的数据。
回答by Sander Marechal
You can use GET variables in the action
parameter of your form
element. Example:
您可以在元素的action
参数中使用 GET 变量form
。例子:
<form method="post" action="script.php?foo=bar">
<input name="quu" ... />
...
</form>
This will give you foo
as a GET variable and quu
as a POST variable.
这将为您foo
提供 GET 变量和quu
POST 变量。
回答by oriadam
If you want to fake a $_GET (or a $_POST) when including a file, you can use it like you would use any other var, like that:
如果您想在包含文件时伪造 $_GET(或 $_POST),您可以像使用任何其他 var 一样使用它,如下所示:
$_GET['key'] = 'any get value you want';
include('your_other_file.php');
回答by Jens A. Koch
One way to set the $_GET
variable is to parse the URL using parse_url()
and then parse the $query
string using parse_str()
, which sets the variables into the $_GET
global.
设置$_GET
变量的一种方法是使用解析 URL parse_url()
,然后使用解析$query
字符串parse_str()
,这将变量设置为$_GET
全局变量。
This approach is useful,
这个方法很有用
- if you want to test GET parameter handling without making actual queries, e.g. for testing the incoming parameters for existence and input filtering and sanitizing of incoming vars.
- and when you don't want to construct the array manually each time, but use the normal URL
- 如果您想在不进行实际查询的情况下测试 GET 参数处理,例如用于测试传入参数的存在以及输入过滤和传入变量的清理。
- 当你不想每次都手动构造数组,而是使用普通的 URL
function setGetRequest($url)
{
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $_GET);
}
$url = 'http://www.example.com/test.php?a=10&b=plop';
setGetRequest($url);
var_dump($_GET);
Result: $_GET
contains
结果:$_GET
包含
array (
'a' => string '10' (length=2)
'b' => string 'plop' (length=4)
)
回答by Theo
The $_GET
variable is populated from the parameters set in the URL. From the URL http://example.com/test.php?foo=bar&baz=buzz
you can get $_GET['foo']
and $_GET['baz']
. So to set these variables, you only have to make a link to that URL.
该$_GET
变量由 URL 中设置的参数填充。从 URLhttp://example.com/test.php?foo=bar&baz=buzz
你可以得到$_GET['foo']
和$_GET['baz']
。因此,要设置这些变量,您只需创建指向该 URL 的链接。
回答by sreekanth
For the form, use:
对于表单,请使用:
<form name="form1" action="<?=$_SERVER['PHP_SELF'];?>" method="get">
and for getting the value, use the get method as follows:
要获取值,请使用 get 方法,如下所示:
$value = $_GET['name_to_send_using_get'];
回答by Joseph Orlando
You could use the following code to redirect your client to a script with the _GET variables attached.
您可以使用以下代码将您的客户端重定向到附加了 _GET 变量的脚本。
header("Location: examplepage.php?var1=value&var2=value");
die();
This will cause the script to redirect, make sure the die();
is kept in there, or they may not redirect.
这将导致脚本重定向,确保将die();
其保留在那里,否则它们可能不会重定向。
回答by SkriptZor
I know this is an old thread, but I wanted to post my 2 cents...
我知道这是一个旧线程,但我想发布我的 2 美分...
Using Javascript you can achieve this without using $_POST, and thus avoid reloading the page..
使用 Javascript,您可以在不使用 $_POST 的情况下实现这一点,从而避免重新加载页面。
<script>
function ButtonPressed()
{
window.location='index.php?view=next'; //this will set $_GET['view']='next'
}
</script>
<button type='button' onClick='ButtonPressed()'>Click me!</button>
<?PHP
if(isset($_GET['next']))
{
echo "This will display after pressing the 'Click Me' button!";
}
?>