drxdai15012937753
2013-03-20 03:07
浏览 47
已采纳

如何在连续提交表单后保留变量的值

I am developing a Contextual Search Engine in php. For that when an user types a query, I need her latlong and time. I am developing the search box in php. To get latlong I am using HTML 5 geolocation api. I have written following two files by taking idea from posts in 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>

Another file is 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>

The problem is that whenever I am submitting the form again from process.php the latitude and longitude values are getting reset. So how do I retain the value of them that I have got after landing on process.php so that they do not get reset even if I submit the form from process.php multiple times.

In this context I have seen other similar questions asked here and applied their solutions but none of them seems to be working. So please help. Thank you.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douguan1887 2013-03-20 03:30
    已采纳

    Inside process.php, you're using GET in order to get the value that's being POSTed from a process.php form submission.

    You can change:

    $latitude=$_GET['latitude'];
    $longitude=$_GET['longitude'];
    

    Into

    $latitude=$_REQUEST['latitude'];
    $longitude=$_REQUEST['longitude'];
    

    $_REQUEST basically contains both GET and POSTs (to be safe, make sure there are no conflicting get/post params with the same key name).

    打赏 评论
  • dqluw20882 2013-03-20 03:27

    You are submitting the first form via the GET method which puts the values in the URL and the second form on the process.php page uses the POST method which puts the values in the body of the request and not the URL.

    You are then getting lat and long from the $_GET super global which no longer holds lat and long as they are now in the $_POST superglobal.

    You should use either GET or POST consistently, and for a search engine, I'd recommend GET so that users can pass around links to results (you can't bookmark a POST'ed page).

    打赏 评论
  • doufei2007 2013-03-20 03:30

    You are looking for the Geolocation values in the POST request when in fact the initial form is sending the values as a GET request.

    Change in process.php the following lines:

    <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; ?>">
    

    to

    <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; ?>">
    
    打赏 评论

相关推荐 更多相似问题