php POST插入到javascript中

时间:2013-12-02 08:56:17

标签: javascript php geocoding

我尝试使用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>

4 个答案:

答案 0 :(得分:2)

我认为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

当您调用address函数时,您声明的变量codeAddress()会被覆盖。 所以也清楚这一行:

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;";
}

?>

...

答案 1 :(得分:1)

只需删除此行:

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

因为它会覆盖你已经在第三行声明的address

答案 2 :(得分:1)

您回复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>

答案 3 :(得分:0)

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

<?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>