php 在html中传递带有href的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13102351/
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 a variable with href in html
提问by Delphinium
I want to pass a variable along with the link on a button click, while using href. can anyone tell me the exact syntax or if it is even possible? i want to do something like this: href="link+variable" so that i can extract the variable and use it on the "link" which will open.
我想在使用 href 时在单击按钮时传递一个变量和链接。谁能告诉我确切的语法,或者甚至可能吗?我想做这样的事情: href="link+variable" 以便我可以提取变量并在将打开的“链接”上使用它。
采纳答案by Lennart
Yes it is possible by doing the following
是的,可以通过执行以下操作
site 1
站点 1
<input type="button" onClick="window.location='http://example.com?var=<?php echo $var ?>'">
On the recieving site use the following:
在接收站点上使用以下内容:
site 2
站点 2
<?php
$var = $_GET['var'];
?>
The variable you sent is store in $var in site 2
您发送的变量存储在站点 2 的 $var 中
回答by Lawrence Cherone
Do you mean passing a value as a parameter?
你的意思是传递一个值作为参数?
<a href="page.php?value_key=some_value">Link</a>
<a href="page.php?value_key=some_value">Link</a>
Then in PHP
然后在 PHP
if(isset($_GET['value_key'])){
$var = $_GET['value_key']; //some_value
}
回答by Jrod
What you are referring to is a query string.
您所指的是查询字符串。
A query string uses name value pairs. The ?is used to start a query string. An &would be used to string multiple variables together.
查询字符串使用名称值对。将?用于启动查询字符串。An&将用于将多个变量串在一起。
<a href="http://www.example.com/index.php?var=value&var2=value2">Pass the variable</a>
These variables would be accessible in php by using either $_GETor $_REQUEST
这些变量可以在 php 中通过使用$_GET或$_REQUEST
On the target php page:
在目标 php 页面上:
$var = $_GET['var'];
$var2 = $_GET['var2'];
回答by Alvar FinSoft Soome
Full description how to handle with external variables is here:
如何处理外部变量的完整描述在这里:

