After retrieving the latitude and longitude coordinates from the maps, I am trying to send the address to another PHP file using Ajax in JavaScript.
Unfortunately, the Ajax is not working properly for me. I am puzzled as both the Ajax code and the JavaScript maps section have been individually tested and are functioning correctly.
<script type="text/javascript">
var latlng = new google.maps.LatLng(<?php echo $lat;?>, <?php echo $long;?>);
var geocoder = geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
var Var_JavaScript = results[1].formatted_address;
window.location.replace("insert-fichaje2.php?lat=<?php echo $lat;?>&long=<?php echo $long;?>&status=<?php echo $status;?>&usuario=<?php echo $usuario;?>&direccion="+Var_JavaScript);
}
}
});
</script>
If I replace window.location.replace
with:
$.ajax({
type: "POST",
url: "insert-fichaje2.php?lat=<?php echo $lat;?>&long=<?php echo $long;?>&status=<?php echo $status;?>&usuario=<?php echo $usuario;?>&direccion="+Var_JavaScript ,
dataType: "json",
success: function() {
alert("Success");
window.close();
},
error: function() {
alert("ERROR!!!");
}
});
then the functionality breaks.
Console error messages:
Uncaught SyntaxError: Unexpected token '<'
insert-fichaje.php?lat=40.394548&long=-3.5861034&status=Pauza&usuario=liviudiaconu:14 Uncaught ReferenceError: $ is not defined
at insert-fichaje.php?lat=40.394548&long=-3.5861034&status=Pauza&usuario=liviudiaconu:14
at geocoder.js:5
at geocoder.js:2
at XS (geocoder.js:2)
at Kp.d [as o] (geocoder.js:2)
at Object.c [as _lby7xg] (VM87105 common.js:68)
at GeocodeService.Search?5m2&1d40.394548&2d-3.5861034&7sUS&9ses-ES&callback=_xdc_._lby7xg&key=AIzaSyBIgTmGmP2ccdBJWMJbH8MYHdxB33tb9AU&token=50596:1