Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-07-05 21:13:59

kevinpotts
Member
From: Ghost Coast
Registered: 2004-12-07
Posts: 370

Basic AJAX / jQuery Help Needed

I have a reservation form that includes a datepicker (using jQuery UI). I need to take the value of that choice, pass it through a tiny PHP script that returns the time of sunset for that day, and load it back into the page. I have a lot of the components complete — I am just not sure how to tie them together.

Here is the jQuery for the datepicker. It works fine, and shows a human readable version of the choice in an alternative field while using the Unix timestamp version of the date for firing events. I even have the beginnings of the ajax call, which I think is correct.

$("#reservationdate").datepicker({
	showAnim: 'slideDown',
	dateFormat: '@',
	showOn: 'button',
	buttonText: 'select date',
	altField: '#alternate',
	altFormat: 'DD, MM d, yy',
	onSelect: function(dateText, inst) { 
		alert(dateText);
		$.ajax({
			url: '/sunset.php',
			data:  { time : dateText },
			method: 'GET',
			dataType: 'text'	
		});
	}
});

Here is the code for sunset.php:

<?php
header("Cache-Control: no-cache");
$latitude  = 31.977794; # -ve is south of Equator, +ve is north
$longitude = -110.301361; # -ve is west of Grenwich meridian, +ve is east.
$sunset = date_sunset ( date( 'U' ), SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude);
echo date( 'g:i a', $sunset );
?>

So the bit where it currently says date( 'U' ) is a placeholder for the value of the query string “time”. I just have no idea how to make the value of the URL go into the PHP, and then call back the results to the actual page.

Any help = massively appreciated.


Kevin
(graphicpush)

Offline

#2 2010-07-05 21:31:16

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,743
Website GitHub

Re: Basic AJAX / jQuery Help Needed

kevinpotts

Nice.

In theory you can use $theTime = gps('time'); because you’ve specified that the data be sent as a GET query string (in fact, if you choose POST it should still work — the ‘p’ in gps = POST :-)

You should then just be able to chuck that variable in where your placeholder is and laugh all the way to the sunset.

One other thing you might consider for any other dabbles with AJAX is TXP’s built in sendAsyncEvent() function. It works pretty much like your version in this case so there’s probably not much advantage to using it, but it can be handy if you’re communicating with a plugin or something because you can package up the data to send (just as you have here) along with a TXP event/step and specify a callback to run when the answer is returned.

There’s an equivalent function on the server side called send_xml_response() that allows you to squirt a nicely-formatted XML stream back to the browser which jQuery can then slice and dice with comparative ease.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2010-07-06 01:33:43

kevinpotts
Member
From: Ghost Coast
Registered: 2004-12-07
Posts: 370

Re: Basic AJAX / jQuery Help Needed

Bloke —

Thanks for the response. I guess I am confused as to what the best route to take. I definitely don’t need XML — all I want to throw back is a simple text value like “7:30 pm”, which is what that clever PHP script currently compiles for me. (For reference, here is the page in question.)


Kevin
(graphicpush)

Offline

#4 2010-07-06 08:36:04

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,743
Website GitHub

Re: Basic AJAX / jQuery Help Needed

kevinpotts

Oops, I forgot to convert the date to a timestamp, sorry.

$theTime = strtotime(gps('time'));
header("Cache-Control: no-cache");
$latitude  = 31.977794; # -ve is south of Equator, +ve is north
$longitude = -110.301361; # -ve is west of Grenwich meridian, +ve is east.
$sunset = date_sunset ( $theTime, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude);
echo date( 'g:i a', $sunset );

The only thing you’ll have to watch is if the date is of a format that strtotime doesn’t understand (or the date is somehow mangled). In that case you’ll get FALSE returned from the strtotime and your sunset output will be banjaxed. You may have to wrap the rest of the code in:

$theTime = strtotime(gps('time'));
header("Cache-Control: no-cache");
if ($theTime !== false) {
   $latitude = ...
} else {
   echo 'Sunset info not available';
}

Confusingly, if you’re using PHP < 5.1.0, the fail return value from strtotime is -1 so you’ll have to change the code to if ($theTime < 0) { ... }

Last edited by Bloke (2010-07-06 08:37:01)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB