0

I'm using geolocation (navigator.geolocation) and need to query my mysql database without doing a page reload. The navigator.geolocation gives me the lat/lon upon loading the page. I then need to access location.php which has my mysql query to get the closest region listed in my table.

The geolocator works as does the mysql query. What I'm guessing is that I need a function like the following to access the location.php file and get the query results back:

$('#location').live('pagebeforecreate',function(event){
});

What I don't know is what the meat of the function should look like.

Help please!

2 Answers 2

1

Perhaps the jQuery Ajax functions could help: http://api.jquery.com/jQuery.ajax/

You're basically just loading one ajax after the other. So in the "success" of the first request, load the second one. It won't happen before the page loads but you can always update the page after it loads.

2
  • Thanks guys. I'm able to retrieve data from the database but I'm having trouble using the returned variable. At the bottom of the code example I inserted "DISPLAY js VAR region here?" where I need to use the region variable. How can I change my code to do that? Here's my code:
    – Tim
    May 14, 2011 at 21:56
  • function handlePosition(pos) { $.ajax({ url: 'map.php', data: "lat=" + pos.coords.latitude + "&lon=" + pos.coords.longitude, dataType: 'json', success: function(data) { region = data[0]; } }); } </script> <select id="cities"> <option value="<?php echo "$group_id"; ?>" <?php if ($mailregion == DISPLAY js VAR region here?)) { echo "selected=\"selected\""; } ?>><?php echo "$mailregion"; ?></option>
    – Tim
    May 14, 2011 at 21:57
0

Mohamed's answer is correct, but you should initiate the query server-side by including it in the script that returns the requested page.

1
  • Thanks guys. I'm almost there. I'm able to retrivee data from the database but I'm having trouble using the returned variable. Here's my code:
    – Tim
    May 14, 2011 at 21:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.