Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-04-2010, 03:22 PM   PM User | #1
Anthony2oo4
Regular Coder

 
Join Date: Jul 2004
Posts: 201
Thanks: 8
Thanked 1 Time in 1 Post
Anthony2oo4 is an unknown quantity at this point
Simple run function on pageload

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.

Last edited by Anthony2oo4; 10-04-2010 at 03:25 PM..
Anthony2oo4 is offline   Reply With Quote
Old 10-04-2010, 06:48 PM   PM User | #2
Lerura
Regular Coder

 
Lerura's Avatar
 
Join Date: Aug 2005
Location: Denmark
Posts: 869
Thanks: 0
Thanked 112 Times in 111 Posts
Lerura will become famous soon enough
Code:
<body onload="ajaxFunction();">
Lerura is offline   Reply With Quote
Old 10-04-2010, 07:20 PM   PM User | #3
Anthony2oo4
Regular Coder

 
Join Date: Jul 2004
Posts: 201
Thanks: 8
Thanked 1 Time in 1 Post
Anthony2oo4 is an unknown quantity at this point
Hi lerura,


I tried that but it wouldn't work.
Anthony2oo4 is offline   Reply With Quote
Old 10-04-2010, 07:45 PM   PM User | #4
Lerura
Regular Coder

 
Lerura's Avatar
 
Join Date: Aug 2005
Location: Denmark
Posts: 869
Thanks: 0
Thanked 112 Times in 111 Posts
Lerura will become famous soon enough
You are also calling ajaxFunction() here:
Code:
...
ajaxFunction();
</script>
hence you are making the call before the page is loaded - Also.

try removing that line and see if the onload will work then.
Lerura is offline   Reply With Quote
Old 10-04-2010, 08:52 PM   PM User | #5
Anthony2oo4
Regular Coder

 
Join Date: Jul 2004
Posts: 201
Thanks: 8
Thanked 1 Time in 1 Post
Anthony2oo4 is an unknown quantity at this point
No it dosnt work that way. I added it there to see if I could solve it but it didnt work. I forgot to take it out.

Thanks for your reply.
Anthony2oo4 is offline   Reply With Quote
Old 10-06-2010, 10:53 AM   PM User | #6
Anthony2oo4
Regular Coder

 
Join Date: Jul 2004
Posts: 201
Thanks: 8
Thanked 1 Time in 1 Post
Anthony2oo4 is an unknown quantity at this point
Anyone else maybe have a suggestion please ?

Thanks
Anthony2oo4 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:18 PM.


Advertisement
Log in to turn off these ads.