phpPOST插入到javascript中


php POST inserted into javascript

我试图使用POST中表单输入的地址,并将该地址插入该javascript中,以便将其地理编码到地图上的一个定点中。我将POST放入了一个javscript变量中,但似乎无法将其放入geodoces函数中。这是测试现场直播http://nickshanekearney.com/geo/

<?php $address = $_POST["address"]; ?>
<script> 
var address = "<?php echo $_POST["address"]; ?>";
var geocoder;
var map;
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    map.setCenter(results[0].geometry.location);
    var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
    });
    infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);
    service.nearbySearch(request, callback);
  } else {
    alert("Geocode was not successful for the following reason: " + status);
  }
});
}
</script>
<div id="map"></div>

我认为您在编写quotes时出错
请参阅下面的行:

var address = "<?php echo $_POST["address"]; ?>";


必须是

 var address = "<?php echo $_POST['address']; ?>";

var address = "<?php echo $address; ?>";// because you already declared variable $address at the top of your source code

当您调用codeAddress()函数时,您声明的变量address将被覆盖。所以也要明确这条线:

var address = document.getElementById("address").value;

或者如果您正在使用它,您必须检查发布的变量是否存在:

function codeAddress(){
<?php 
if (isset($_POST['address'])){
echo "var address = $_POST['address'];"; 
}
else{
echo "var address = document.getElementById('address').value;";
}
?>
...

只需删除此行:

var address = document.getElementById("address").value;

因为它覆盖了您已经在第三行声明的address

奇怪的是,您将值回显到JS,但您的JS从ID为address的元素中获取地址,覆盖了您的PHP回显结果。

你可能想要:

function codeAddress() {
  var address = "<?php echo $_POST['address']; ?>";
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
      });
      infowindow = new google.maps.InfoWindow();
      var service = new google.maps.places.PlacesService(map);
      service.nearbySearch(request, callback);
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}
</script>

您可以将地址作为参数传递给函数,如下所示:

<?php $address = $_POST["address"]; ?>
<script> 
var address = "<?php echo $_POST["address"]; ?>";
var geocoder;
var map;
function codeAddress(address) {
geocoder.geocode( { 'address': address}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    map.setCenter(results[0].geometry.location);
    var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
    });
    infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);
    service.nearbySearch(request, callback);
  } else {
    alert("Geocode was not successful for the following reason: " + status);
  }
});
}
</script>
<div id="map"></div>