将数据从JS传递到PHP无法正常工作

时间:2019-10-23 18:21:09

标签: php

我有一个自动完成的googleapi,可以正确获取纬度,经度和地址。当我尝试将这些值传递给PHP时,我对Java的无知使我做错了事,因为php没有接收到任何数据。

JS

 <h1>Autocomplete form</h1>
 <input id="searchMapInput" class="mapControls" type="text" placeholder="- Type your location here -">
 <ul id="geoData">
 <li>Full Address: <span id="location-snap"></span></li>
 <li>Latitude: <span id="lat-span"></span></li>
 <li>Longitude: <span id="lon-span"></span></li>
 </ul>

 <script>
 function initMap() {
 var input = document.getElementById('searchMapInput');

 var autocomplete = new google.maps.places.Autocomplete(input);

autocomplete.addListener('place_changed', function() {
    var place = autocomplete.getPlace();
    document.getElementById('location-snap').innerHTML = place.formatted_address;
    document.getElementById('lat-span').innerHTML = place.geometry.location.lat();
    document.getElementById('lon-span').innerHTML = place.geometry.location.lng();

   function redirectToPosition(position) { 
   window.location='a.php?lat='+place.geometry.location.lat+'&long='+place.geometry.location.lng;
   }      
   });
   }
   </script>

   <script src="https://maps.googleapis.com/maps/api/js?key=[API_KEY_goes here]&libraries=places&callback=initMap" async defer>
   </script>

服务器端PHP是这个(注释后编辑)

<?php
$latitude = (isset($_GET['lat']))?$_GET['lat']:'';
$longitude = (isset($_GET['long']))?$_GET['long']:'';
echo "Latitude:".$latitude."</br>";
echo "Longitude:".$longitude;
?>

我得到了网址 --- / a.php?lat = function%20(){return%20a}&long = function%20(){return%20b}

和以下回显:

纬度:函数(){返回a}

经度:函数(){return b}

1 个答案:

答案 0 :(得分:0)

您应检查$_GET['lat']$_GET['long']

<?php
$latitude = (isset($_GET['lat']))?$_GET['lat']:'';
$longitude = (isset($_GET['long']))?$_GET['long']:'';
echo "Latitude:".$latitude."</br>";
echo "longitude:".$longitude;
?>