Pass javascript value to php post

Asked

Viewed 117 times

0

I’m having trouble picking up my location and moving on to the php post with onload. How can I do that ? The values I’m trying to capture are the lat and long google.

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: {lat: -34.397, lng: 150.644},
    zoom: 6
  });
  var infoWindow = new google.maps.InfoWindow({map: map});

  // Try HTML5 geolocation.
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };

      infoWindow.setPosition(pos);
      infoWindow.setContent('Location found.');
      map.setCenter(pos);
    }, function() {
      handleLocationError(true, infoWindow, map.getCenter());
    });
  } else {
    // Browser doesn't support Geolocation
    handleLocationError(false, infoWindow, map.getCenter());
  }
}
  • Make an asynchronous request by passing the values, if you are using jquery, you can use the $.post

  • but what can’t you do? Get the location(lat, lng) of google maps, or send this data to php?

  • pass the coordinates to php.

1 answer

0

An example of how you can do with jquery.ajax (you have documentation on the official website) api.jquery.com/jquery.ajax/

$.ajax({
  method: "POST",
  url: "some.php",
  data: {lat: -34.397, lng: 150.644}
})
.done(function( msg ) {
  alert( "Data Saved: " + msg );
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.