php 将参数传递给没有表单的php页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5107811/
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
Passing parameter to php page without form
提问by Captain Comic
I am new to PHP.
我是 PHP 新手。
I have a page that displays user profile. I need to pass user id to this page so that correct profile was displayed.
我有一个显示用户个人资料的页面。我需要将用户 ID 传递到此页面,以便显示正确的个人资料。
I just dont use the <form>
element. I want to have a link
我只是不使用该<form>
元素。我想要一个链接
<a href="/users/24378234298734">
or <a href="/users/?id=24378234298734">
or whatever
<a href="/users/24378234298734">
或<a href="/users/?id=24378234298734">
或什么
Since I am not using form I cannot use _GET or _POST on the handler page What is the best way to handle the parameters on handler page?
由于我没有使用表单,因此无法在处理程序页面上使用 _GET 或 _POST 在处理程序页面上处理参数的最佳方法是什么?
回答by Quentin
A form with method="GET"
is just a way to build a query string automatically based on user input. Nothing prevents you using $_GET
to read data from a manually constructed query string (and the server can't tell the difference anyway).
表单 withmethod="GET"
只是一种根据用户输入自动构建查询字符串的方法。没有什么可以阻止您使用$_GET
从手动构造的查询字符串中读取数据(并且服务器无论如何都无法区分)。
<a href="/users/?id=24378234298734">
will cause $_GET['id']
to be populated.
<a href="/users/?id=24378234298734">
将导致$_GET['id']
被填充。
回答by powtac
If you have a link somewhere like
如果你在某个地方有一个链接
<a href="/users.php?id=24378234298734">User XY</a>
and you put this code on users.php:
你把这段代码放在 users.php 上:
echo 'Hello '.$_REQUEST['id']; // $_REQUEST catches $_GET and $_POST
you will be able to set up a user page for user number 24378234298734.
您将能够为用户号 24378234298734 设置一个用户页面。
回答by Dutchie432
Use this..
用这个..
HTML
HTML
<a href="/users/?id=24378234298734">Link</a>
PHP
PHP
$id = $_REQUEST['id'];
回答by Joe Green
You can use $_GET['id'] to retrieve the value in the url /users/?id=324332. No forms are required to receive _GET variables.
您可以使用 $_GET['id'] 来检索 url /users/?id=324332 中的值。不需要表单来接收 _GET 变量。