2

This is my first time using AJAX, and I'm trying to send JS variables to a PHP script. I've got an XMLHttpRequest but it doesn't seem complete - what am I missing?

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
    }
    else {
        document.write("Geolocation is required for this page.");
    }

    function successFunction(position) {
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;
    //    document.write("<a href='http://api.geonames.org/findNearbyPlaceNameJSON?lat="+lat+"&lng="+lng+"&username=sebastiano'>my town</a>");

        var xmlhttp;
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else {
            // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.open("GET","location.php?lat=position.coords.latitude",true);
        xmlhttp.send();

        // SOMETHING MISSING HERE?

    }

    function errorFunction(position) {
        document.write("Error");
    }
3
  • If this is your first time dealing with Ajax, I reccomend you not to do it by your self and use a common simple library such as jquery.com you will be able to make your application much more reacher. Commented Jul 6, 2011 at 13:18
  • Thanks for your reply, are there any tutorials or search terms I can try to make this easier? Commented Jul 6, 2011 at 13:19
  • net.tutsplus.com/tutorials/javascript-ajax/… is a good overview, otherwise just serach for jquery ajax. Commented Jul 6, 2011 at 14:46

3 Answers 3

3

It appears that you're not passing the contents of your variable to the open command.

xmlhttp.open("GET","location.php?lat=position.coords.latitude",true);

in this example your lat will contain a string with contents "position.coords.latitude"

instead try

xmlhttp.open("GET","location.php?lat="+position.coords.latitude,true);

Or better yet, use the variables you created at the top of the function and pass both long and lat in.

xmlhttp.open("GET","location.php?lat=" + lat + "&long=" + lng,true);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I hadn't noticed that. How can I tell if my external PHP is receiving these values?
Quickest way, not necessarily the best, would be to set up a function in the location.php page that emails you the contents of the two variables.
Is there a way of printing it in the window of the index file?
You can echo the values in your location.php file and then output the response in your index file. You'll need an event handler to listen for xmlhttp request complete which will give you the response data
2

you are sending it with "position.coords.latitude" as value...

try xmlhttp.open("GET","location.php?lat=" + position.coords.latitude,true);

Also, take a look at jQuery

Comments

1

Try this:

        var xmlhttp;

        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){
                        console.log(xmlhttp.responseText);
                }
        }

        xmlhttp.open("GET","location.php?lat=" + position.coords.latitude,true);
        xmlhttp.send();

1 Comment

here you need to echo the result inside location.php, then this code will log it to console. you can use it as string (i.e. you can alert etc. if you like).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.