我正在php中开发一个上下文搜索引擎。为此,当用户键入查询时,我需要她的latlong和time。我正在php中开发搜索框。为了获得latlong,我使用HTML5地理定位api。我从stackoverflow的帖子中获得了这个想法,写了以下两个文件。
order.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表单提交中发布的值。
您可以更改:
$latitude=$_GET['latitude'];
$longitude=$_GET['longitude'];
进入
$latitude=$_REQUEST['latitude'];
$longitude=$_REQUEST['longitude'];
$_REQUEST
基本上同时包含GET和post(为了安全起见,请确保没有具有相同密钥名称的冲突的GET/post参数(。
您通过GET方法提交第一个表单,该方法将值放入URL中,而process.php页面上的第二个表单使用POST方法,该方法会将值放入请求的正文中,而不是URL中。
然后从$_GET
超全局中获取lat和long,该超全局不再保持lat和long,因为它们现在在$_POST
超全局中。
您应该一致地使用GET或POST,对于搜索引擎,我建议使用GET,以便用户可以传递指向结果的链接(您不能将POST页面作为书签(。
您在POST请求中查找地理位置值,而实际上初始表单正在将这些值作为GET请求发送。
process.php
中以下行的更改:
<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 name="latitude" id="latitude" type="hidden" value="<?php $latitude=$_REQUEST['latitude']; echo $latitude; ?>">
<input name="longitude" id="longitude" type="hidden" value="<?php $longitude=$_REQUEST['longitude'];echo $longitude; ?>">