php 使用 <a href= > 标签传递值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9269107/
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 values using <a href= > tag
提问by user1135192
I have an array of coordinates (latitude and longitude) in one .php script and want to pass these values to maps.php which will display a google map and plot these values passed (i.e. latitude and longitude coordinates) on the map.
我在一个 .php 脚本中有一个坐标数组(纬度和经度),并希望将这些值传递给 maps.php,它会显示一个谷歌地图并在地图上绘制这些传递的值(即纬度和经度坐标)。
My question is that, is it possible to pass these values to maps.php when clicking on
<a href="maps.php"> view map </a>
???
我的问题是,是否可以在单击时将这些值传递给 maps.php
<a href="maps.php"> view map </a>
???
Thanks
谢谢
回答by Julio
You can serialize your array, and then pass it in a GET parameter. For instance:
您可以序列化您的数组,然后将其传递到 GET 参数中。例如:
# Your array of coordinates
$coord_array = array();
# Serialize the coordinates
$coord_array = serialize(coord_array);
# In your href you'd have
print '<a href="maps.php?coords=' . $coord_array . '">View Map</a>';
Now in your maps.php, you need to unserialize, and you can interact with the array as you normally would:
现在在你的 maps.php 中,你需要反序列化,你可以像往常一样与数组交互:
# Get the information from the URL
$coord_array = $_GET['coords'];
# Unserialize
$coord_array = unserialize(coord_array);
# Check the input to make sure it hasn't been changed . . .
# Now interact as you normally would with the array
print_r($coord_array);
This method would be ideal for passing a lot of values in a single GET parameter. If you're passing only one value, then you may be better off just setting two GET parameters, one for the longitude and one for the latitude.
此方法非常适合在单个 GET 参数中传递大量值。如果您只传递一个值,那么最好只设置两个 GET 参数,一个用于经度,另一个用于纬度。
Also note, that I didn't test any of this code as I didn't have time, but the concepts should be correct.
另请注意,由于我没有时间,我没有测试任何这些代码,但这些概念应该是正确的。
回答by fivedigit
In your HTML do this:
在您的 HTML 中执行以下操作:
<a href="maps.php?param1=value1&param2=value2">view map</a>
In your PHP code you can get the values like this:
在您的 PHP 代码中,您可以获得如下值:
$param1 = $_GET['param1'];
$param2 = $_GET['param2'];
Read more on GET parameters in the PHP documentation.