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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组