Often after a db query I will echo out a table, in the last column I add something like this, so when clicking the href I can run some php that will get the row ID and I can then manipulate that row.
echo '<td><a href="driver-accept-job.php?id=' . $row['booking_id'] . ' "class="btn btn-primary btn-block" " " onclick="return confirm(\'Are you sure you want to accept this job?\')" >Accept Job</a></td>';
Now I would like to burn geolocation data to db when user clicks 'accept job', (if user declines geolocation prompt then will not be able to accept job) since geolocation is js, I believe I now have to use ajax to send the lat and long and the booking_id . I have no idea how to get the $row['booking_id'] and pass it to the js.
This is what I have so far...
echo '<td><button class="btn btn-primary btn-block" onclick="getLocation()" >Accept Job</button></td>';
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
// x.innerHTML = "Latitude: " + position.coords.latitude +
// "<br>Longitude: " + position.coords.longitude;
$.ajax({
type: 'POST',
url: 'driver-accept-job.php',
data: { id: booking_id, latitude: position.coords.latitude, longitude: position.coords.longitude },
success: function(response) {
content.html(response);
}
});
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
</script>
driver-accept-job.php
$id = $_POST['id'];
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];