通过ajax和jquery将变量发送到php

时间:2015-06-03 11:12:06

标签: php jquery ajax variables geolocation

我想通过html5-geolocation请求用户地理位置并将其发送到下一页 我被告知我要使用ajax / jquery,所以这是我的代码:

<form action="response.php">
        <button onclick="getLocation()">Start</button>

<script>

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(success, error);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function successFunction(position) {
   var lat = position.coords.latitude;
   var longi = position.coords.longitude;

    $.ajax({
  type: "POST",
  url: "response.php",
  data: { latitude: lat, longitude: longi }
  }).done(function( msg ) {
  alert( "Data Saved: " + msg );
});
}
</script>
</form>

现在我想在echo.php页面上“回显”纬度和经度 但我不知道该怎么做:'( 我试过这个:

$latitude = $_POST["latitude"]; 
echo $latitude; 

但页面为空白

2 个答案:

答案 0 :(得分:1)

试试这个:

<强>的index.html

<html>
<head></head>
<body>
    <form action="response.php" method="post">
      <input type="hidden" id="latitude" name="latitude" value="" />
      <input type="hidden" id="longitude" name="longitude" value="" />
      <input type="submit" value="Start" />
    </form>
</body>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
    function getLocation() {

         var options = {
            enableHighAccuracy: true,
            timeout: 5000,
            maximumAge: 0
         };

        function success(pos) {
           successFunction(pos);
        };

        function error(err) {
            console.warn('ERROR(' + err.code + '): ' + err.message);
        };

        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(success, error,options);
        } else { 
            //x.innerHTML = "Geolocation is not supported by this browser.";
        }
    }

    function successFunction(position) {
       var lat = position.coords.latitude;
       var longi = position.coords.longitude;

        $('#latitude').val(lat);
        $('#longitude').val(longi);
    }
    getLocation();
</script>
</html>

<强> response.php

<?php
    $latitude = $_POST["latitude"]; 
    $longitude = $_POST["longitude"]; 
    echo "Latitude:".$latitude."</br>";
    echo "longitude:".$longitude;
?>

只需创建 index.html 即可复制并粘贴此代码,并确保 response.php 像这样。

/yourprojectdirectory/index.html
/yourprojectdirectory/response.php

答案 1 :(得分:0)

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(success, error);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function successFunction(position) {
   var lat = position.coords.latitude;
   var longi = position.coords.longitude;

    $.ajax({
  type: "POST",
  url: "response.php",
  data:"data="+ '{ "latitude":"'+ lat+'", "longitude": "'+longi+'" }'
  }).done(function(msg) {
  alert( "Data Saved: " + msg );
});
}
</script>




$location =json_decode( preg_replace('/\\\"/',"\"",$_POST['data'])); 
print_r($location);
 $lat=$location->latitude;
 echo $lan=$location->longitude;