0

PHPでコンテキスト検索エンジンを開発しています。そのためには、ユーザーがクエリを入力するときに、彼女の緯度と時間が必要です。私はphpで検索ボックスを開発しています。緯度経度を取得するには、HTML 5 地理位置情報 API を使用しています。stackoverflow の投稿から着想を得て、次の 2 つのファイルを作成しました。

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 に到達した後に取得した値を保持するにはどうすればよいですか。

この文脈で、私は他の同様の質問がここで尋ねられ、それらの解決策を適用したのを見てきましたが、どれも機能していないようです. だから助けてください。ありがとうございました。

4

3 に答える 3

2

process.php内では、process.phpフォーム送信からPOSTされている値を取得するためにGETを使用しています。

あなたになら変えられる:

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

の中へ

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

$_REQUEST基本的にGETとPOSTの両方が含まれています(安全のため、同じキー名で競合するget / postパラメーターがないことを確認してください)。

于 2013-03-20T03:30:57.677 に答える
0

実際には初期フォームが値をGETリクエストとして送信しているときに、POSTリクエストでジオロケーション値を探しています。

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; ?>">
于 2013-03-20T03:30:57.787 に答える
0

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

于 2013-03-20T03:27:23.837 に答える