Alright, so I'm having some issues on trying to find out how to pass some data that i have saved in localStorage over to a php script that I wrote, so I can then send that over to my database on a server. I did find some code earlier, (https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest), that looked like it would work but I had no luck with it.
Here's the code where I am saving the data and than trying to pass it over my phpscript
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(initialize, showError, takeSnap);
}
else {
alert("Geolocation is not supported by this browser.");
}
}
function initialize(position) {
var lat = position.coords.latitude,
lon = position.coords.longitude;
var mapOptions = {
center: new google.maps.LatLng(lat, lon),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: true
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lon),
map: map,
title: "Current Location"
});
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
alert("User denied the request for Geolocation.");
break;
case error.POSITION_UNAVAILABLE:
alert("Location information is unavailable.");
break;
case error.TIMEOUT:
alert("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
alert("An unkown error occurred.");
break;
}
}
function storeLocal(position) {
if (typeof (Storage) !== "undefined") {
var lat = position.coords.latitude,
lon = position.coords.longitude;
localStorage.latitude = lat;
localStorage.longitude = lon;
}
else {
alert("Your Browser doesn't support web storage");
}
return
}
function snapShot() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(storeLocal, showError);
}
else {
alert("Geolocation is not supported by this browser.");
}
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("post", "snap.php?lat=" + localStorage.latitude + "&lon=" + localStorage.longitude, true);
oReq.send();
}
function reqListener() {
console.log(this.reponseText);
}
This is they script I wrote to save values into database
<?php
// Connecting to the database
mysql_connect("localhost", "username", "password");
mysql_select_db("db_name");
$latitude = mysql_real_escape_string($_GET["lat"]);
$longitude = mysql_real_escape_string($_GET["lon"]);
// Submit query to insert new data
$sql = "INSERT INTO locationsTbl(locID, lat, lon ) VALUES( 'NULL', '". $latitude ."', '". $longitude . "')";
$result = mysql_query( $sql );
// Inform user
echo "<script>alert('Location saved.');</script>";
// Close connection
mysql_close();
?>