Complementing Pedro Augusto's answer, but using the code snippet you posted, the easiest would be to do the post in the showPosition()
function, though in this case maybe it should rename it treatPosition()
or something like this:
var x = document.getElementById("demo");
function getLocation() {
if (! navigator.geolocation) {
x.innerHTML = "Geolocation is not supported by this browser.";
} else {
navigator.geolocation.getCurrentPosition(function (pos) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
$.ajax({
url: "path/to/service.php",
type: "POST",
data: {
lat: position.coords.latitude,
long: position.coords.longitude,
text: x.innerHTML
}
});
});
}
}
Of course, the code of truth depends on the characteristics of the service: what it expects with respect to method, parameter names, what the URL is, what the MIME type of the request, etc.