I am basically trying to store the location of a user at the same time as when I store other information they provide. The location information, latitude and longitude, is provided by JavaScript from Google geolocation.
So the issue here is how to merge the info. I have been successful in simply obtaining the users location and storing that info into mysql without associating it with a name. However, I want to store the location data with a name provided in a form. In the script below, I am trying to pass the name to a javascript function. It does not seem to be working.
It currently seems like my code is not calling the storePosition(name) function. However, the alert with the correct $_REQUEST['name'] is being displayed. thanks for your help.
//call javascript function to store user position
echo "<script type=text/javascript>alert('trying to get to store position, name is {$_REQUEST['name']}');";
echo "storePostion('{$_REQUEST['name']}');";
echo "</script>";
Below is the javascript function I am trying to call from PHP above.
function storePosition(name)
{
alert("made it to storePosition);
var gl = null;
//Simple Javascript code that checks if the browser has the Geolocation API implemented or it has the Google Gears to create the Geolocation object and then uses it to get the current position of the device.
//WC3 Geolocation API
function displayPosition(position) {
p = document.getElementById("p");
p.innerHTML = "<table border='1'><tr><th>Timestamp</th><td>"+ position.timestamp +
"<tr><th>Latitude (WGS84)</th><td>" + position.coords.latitude + " deg</td></tr>" +
"<tr><th>Longitude (WGS84)</th><td>" + position.coords.longitude + " deg</td></tr></table>";
alert("trying to store position coords ... lat: " + position.coords.latitude + "lng: " + position.coords.longitude );
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","savePosition.php?lat=" + position.coords.latitude + "&lng=" + position.coords.longitude + "name=" + name,true);
//+ "&lat=" + position.coords.latitude + "&lng=" + position.coords.longitude
xmlhttp.send();
}
function displayError(positionError) {
alert("error")
}
try {
if(typeof(navigator.geolocation) == 'undefined'){
gl = google.gears.factory.create('beta.geolocation');
} else {
gl = navigator.geolocation;
}
}catch(e){}
if (gl) {
gl.getCurrentPosition(displayPosition, displayError);
} else {
alert("I'm sorry, but geolocation services are not supported by your browser.");
}