Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post

    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.

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    894
    Thanks
    0
    Thanked 117 Times in 116 Posts
    Code:
    <body onload="ajaxFunction();">

  • #3
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    Hi lerura,


    I tried that but it wouldn't work.

  • #4
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    894
    Thanks
    0
    Thanked 117 Times in 116 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    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.

  • #6
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    Anyone else maybe have a suggestion please ?

    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •