我正在用 php 开发一个上下文搜索引擎。为此,当用户键入查询时,我需要她的经纬度和时间。我正在用 php 开发搜索框。为了获得经纬度,我使用 HTML 5 地理定位 api。我通过从 stackoverflow 中的帖子中汲取灵感,编写了以下两个文件。
订单.php
<html>
<head>
<script type="text/javascript">
function getLocation(){
var x = document.getElementById("demo");
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML="Geolocation is not supported by this browser.";
}
}
function showPosition(position){
var latitude=document.getElementById("latitude"),
longitude=document.getElementById("longitude");
latitude.value = position.coords.latitude;
longitude.value = position.coords.longitude;
}
</script>
</head>
<body onload="getLocation()">
<p id="demo"></p>
<form id="searchbox" action="process.php" method="get">
<input name="q" type="text" placeholder="Type here">
<input name="latitude" id="latitude" type="hidden">
<input name="longitude" id="longitude" type="hidden">
<input id="submit" type="submit" value="Search">
</form>
</body></html>
另一个文件是process.php
<html>
<body>
<form id="searchbox" action="process.php" method="post">
<input name="q" type="text" placeholder="Type here">
<input name="latitude" id="latitude" type="hidden" value="<?php $latitude=$_POST['latitude']; echo $latitude; ?>">
<input name="longitude" id="longitude" type="hidden" value="<?php $longitude=$_POST['longitude'];echo $longitude; ?>">
<input id="submit" type="submit" value="Search">
</form>
<?php $quantity=$_POST['quantity'];
$date = date('Y-m-d H:i:s');
$arr=explode(" ",$date);
$latitude=$_GET['latitude'];
$longitude=$_GET['longitude'];
echo "Latitude=". $latitude."Longitude=". $longitude." Time=".$arr[1]."<br>";
?>
</body>
</html>
问题是,每当我从 process.php 再次提交表单时,纬度和经度值就会重置。那么,如何保留登陆 process.php 后获得的它们的值,以便即使我多次从 process.php 提交表单,它们也不会被重置。
在这种情况下,我看到了这里提出的其他类似问题并应用了他们的解决方案,但它们似乎都不起作用。所以请帮忙。谢谢。
最佳答案
在 process.php 中,您使用 GET 来获取从 process.php 表单提交中 POST 的值。
您可以更改:
$latitude=$_GET['latitude'];
$longitude=$_GET['longitude'];
进入
$latitude=$_REQUEST['latitude'];
$longitude=$_REQUEST['longitude'];
$_REQUEST
基本上包含 GET 和 POST(为了安全起见,请确保具有相同键名的 get/post 参数没有冲突)。
关于php - 如何在连续提交表单后保留变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15514570/