views:

51

answers:

2

Hi,

I've been experimenting with a simple project that essentially just stores the number of visits to the sites, along with the latitude / longitude of the user (it's mainly for people visiting via the iPhones Safari browser).

I've managed to store the 'clicks' to the page via PHP / MySQL, and I can display the users lat / long via the navigator.geolocation object in Javascript, but I have no idea how I can then store that (I'm fairly new at this!).

I appreciated that JavaScript is client-side and PHP is server-side, so I presume I need to somehow pass the returned values of my Javascript back to the PHP code somehow?

Thanks for any advice!

+1  A: 

You presume well. You can either put them in hidden fields in a form and ask user to press the button to send data to serve, or you can initiate AJAX call.

Mchl
I'd prefer to avoid buttons where I can... Not to sound stupid, but like I say, I'm fairly new at this; what do you mean by initiating an AJAX call? Could you give me some pointers as to what I should be looking at doing?
dodgrile
+2  A: 

I've not used the navigator.geolocation object, but you should be able to send what it returns to an external page using AJAX.

<script>
var data = 'something-here'; // the result of your geolocation call
$.ajax({
    type: 'POST',
    url: 'http://yourdomain.com/storeLatLngInDb.php',
    data: data
});
</script>

The above is an example written on top of the jQuery framework.

Martin Bean
Thanks, I think that's fairly clear on what I need to be doing. I'll give your solution a shot.
dodgrile