Goodafternoon!
What im trying to do is get the users location (from the iPhone) which I have working and store it in a MySQL database (semi works).
My problem is that to get the location, you need to use javascript. So I thought of using Ajax to post that back to the server. I have very little knowledge in Javascript, and Ajax, so this has been a bit of a struggle. Iv tried to learn how to do it with a tutorial:
http://www.tizag.com/ajaxTutorial/ajax-javascript.php
And this is what I got:
PHP Code:
<?php
function is_iPhone($agent='') {
if(empty($agent)) $agent = $_SERVER['HTTP_USER_AGENT'];
if(!empty($agent) and preg_match("~Mozilla/[^ ]+ \((iPhone|iPod); U; CPU [^;]+ Mac OS X; [^)]+\) AppleWebKit/[^ ]+ \(KHTML, like Gecko\) Version/[^ ]+ Mobile/[^ ]+ Safari/[^ ]+~",$agent,$match)) {
return "YES";
} elseif(stristr($agent,'iphone') or stristr($agent,'ipod')){
return "MAYBE";
} else {
return "NO";
}
}
echo is_iPhone();
?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>iPhone 3.0 geolocation demo</title>
<meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" name="viewport"/>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.myForm.time.value = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "UpdateLog.php?lat=" + Lat + "&long=" + Long + "&acc=" + Acc, true);
ajaxRequest.send(null);
}
//-->
var Lat = ""
var Long = ""
var Acc = ""
function handler(location) {
var message = document.getElementById("message");
message.innerHTML ='<img src="http://maps.google.com/maps/api/staticmap?center=' + location.coords.latitude + ',' + location.coords.longitude + '&zoom=12&size=400x400&markers=color:blue|label:S|'+ location.coords.latitude +','+ location.coords.longitude +'&sensor=true" alt="some_text"/>';
message.innerHTML+="<p>Longitude: " + location.coords.longitude + "</p>";
message.innerHTML+="<p>Latitude: " + location.coords.latitude + "</p>";
message.innerHTML+="<p>Accuracy: " + location.coords.accuracy + "</p>";
Lat = location.coords.latitude
Long = location.coords.longitude
Acc = location.coords.accuracy
}
navigator.geolocation.getCurrentPosition(handler);
ajaxFunction();
</script>
<div id="message">Location unknown</div>
<a href="#" onclick="ajaxFunction();" >Send</a>
</body>
</html>
Now this works if the user clicks on "Send" that I made, but I want it to do it without the user having to click send.
If anyone could help me with this, or point me in a different direction that would be better then your help would be greatly appreciated.
Thanks in advance for your help.
EDIT: The PHP side of this works, that is something I know about lol.