Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Im trying to use the address from a form input that is in the POST and insert that adress into this javascript for geocoding it into a pin point on the map. I put the POST into a javscript variable but i cant seem to get that into the function that geodoces it. here is it live on a test site 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>
share|improve this question
 
see the double quotes (") here var address = "<?php echo $_POST["address"]; ?>";. It's an error writing –  Oki Erie Rinaldi 2 days ago
 
Try to replace with var address = "<?php echo $_POST['address']; ?>"; –  Oki Erie Rinaldi 2 days ago
 
with double quotes , it works –  Charaf jra 2 days ago
add comment

4 Answers

I think you have an error in quotes writing.
See your line below:

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


it must be :

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

or

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

And the variable address that you declared is overwritten when you call codeAddress() function. So clear this line too :

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

Or if you are using it,you have to check if the posted variable exist or not :

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

?>

...
share|improve this answer
 
var address = "<?php echo $_POST["address"]; ?>"; this works too –  Charaf jra 2 days ago
add comment

Just remove this line:

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

Because it overwrites the address you already declared on the third line.

share|improve this answer
add comment

It is so strange that you echo the value to JS, but your JS gets the address from element with ID = address, overwritten your PHP-echoed result.

You probably want:

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>
share|improve this answer
add comment

You can pass the address as argument to your function like this :

<?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>
share|improve this answer
 
This is all good advice im so close you can see here my progress nickshanekearney.com/geo It works now but not on load I have to press the submit button twice. is it because I need to start the function on load instead of on submit of form? –  user3029877 2 days ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.