Take a lat/long pair from JavaScript for use in date_sunset in PHP
I have decided i'd like to do an ambitious mood change depending on the time of day. I need to get the time and compare it against the sunset and sunrise of any given day.
So far I have found some j开发者_JAVA百科avascript here that works well. Can anyone tell me how I might give the lat and long to the PHP function date_sunset? I have the IP in a PHP variable using $_SERVER['REMOTE_ADDR']
but that's all I have. I can't really do much else as I have never used the function and never tried to combine PHP and JS. Any ideas?
-- Update --
I have been trying to use the jQuery $.ajax function. Here is my code;
<script language="JavaScript">
var lat = geoip_latitude();
var long = geoip_longitude();
$(document).ready(function(){
//var url = 'http://www.ransomedesign.co.uk/web/?lat=' + lat + '&long='+ long;
// $.get(url);
$.ajax({
type: "GET",
data: { lat:lat, long:long},
url: "http:www.ransomedesign.co.uk/web/",
success: function() { alert("you win"); }
})
});
</script>
There are two different approaches in there. (1st one is commented out). This does populate my $_SERVER php code, but only on the 2nd request of a page for my site. I really need this to process on the first visit before anything loads. After this the value will be stored in a session and php will do the rest.
Can anyone see whats wrong with this code or why it wont work straight away? My php if after this, is that correct?
Many thanks...
Given that snippet there, you'd have two variables, geoip_latitude
, and geoip_longitude
. Take those variables, put them into an AJAX call to your server, which would then do the calculations:
html:
<div id="sunset"></div>
<script>
$('#sunset').load('/path/to/script.php?lat=' + geoip_latitude + '&lon=' & geoip_longitude);
</script>
php:
<?php
$lat = $_GET['lat'];
$lon = $_GET['lon'];
echo date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long);
This assumes you've got jquery loaded/available and the server's on PHP v5. I'm also using the server's time, which could be on a different day than the client entirely. Probably won't work out of the box, but should get you started.
I'd use AJAX for this. You use GeoIP JavaScript, so first get lat
and long
:
var lat = geoip_latitude () ;
var long = geoip_longitude () ;
then send it to server script, for example mood.php
:
var xhr = new XMLHttpRequest () ;
xhr . onreadystatechange = function ()
{
if ( xhr . readyState == 4 && xhr . status == 200 )
{
// do something with server response
}
}
xhr . open ( "POST", "mood.php", true ) ;
xhr . setRequestHeader ( "Content-type", "application/x-www-form-urlencoded" ) ;
xhr . send ( "lat=" + lat + "&long=" + long ) ;
and mood.php might looks like:
<?php
$lat = $_POST [ "lat" ] ;
$long = $_POST [ "long" ] ;
echo "something depending on lat and long" ;
?>
edit.
If server sends response, you can read it by calling xhr . responseText
:
xhr . onreadystatechange = function ()
{
if ( xhr . readyState == 4 && xhr . status == 200 )
{
alert ( "Server response: " + xhr . responseText ) ;
}
}
If server sends XML content, then you can call xhr . responseXML
- it tiggers native XML parser.
https://developer.mozilla.org/en/AJAX/Getting_Started
This question had no answer as such, I ended up getting a PHP module and .dat file installed on my hosting for me. The AJAX did work, but only after a page from my site had been visited - which was no use sadly. Thanks for the help everyone..
精彩评论