Struggling to figure out how to effectively pass a Javascript Variable to Ajax and then post it to PHP? While both the Javascript and PHP code are functioning as expected, the challenge lies in transferring the Javascript Variable to Ajax for subsequent posting to PHP. Take a look at the code snippet below:
<!-- User Location -->
<script>
window.onload = function() {
var startPos;
var geoSuccess = function(position) {
startPos = position;
document.getElementById('startLat').innerHTML = startPos.coords.latitude;
document.getElementById('startLon').innerHTML = startPos.coords.longitude;
$.ajax({
type: 'POST',
url: 'MyDashboard.php',
data: 'latitude='+latitude+'&longitude='+longitude,
success: function(msg) {
if (msg) {
$("#location").html(msg);
} else {
$("#location").html('Not Available');
}
}
});
};
navigator.geolocation.getCurrentPosition(geoSuccess);
};
</script>