Using SMF's AJAX

Started by Sarke, June 24, 2007, 03:34:35 PM

Previous topic - Next topic

Sarke

Hi all, I'm trying to figure out how to use SMF's AJAX framework for other stuff on the forum, but haven't been able to get my head around it yet.

I'm not too concerned with the PHP side, but JS has never been my strong point.  It obviously has something to do with these two:

// Load an XML document using XMLHttpRequest.
function getXMLDocument(url, callback)


// Send a post form to the server using XMLHttpRequest.
function sendXMLDocument(url, content, callback)



What I'm looking to do is return a search result from a PHP array, but I'm not sure how to tie the above JS into that.  If someone could point me to the right area of SMF that uses something like that it would be great so I can figure it out, or maybe some example code, it would be appreciated.

My MODs          Please don't PM me for support, post in the appropriate topic.

Rudolf

#1
You can use my Enhanced Password Reminder for a very basic usage of the AJAX framework.
Basically what you need is the sendXMLDocument(url, content, callback) function in your js and the callback to do what needs to be done once the results arrive.
In my mod the submitReminder() and the onReminderReceived(XMLDoc) function are the clue parts. One is where I make the request to the server, the latter is the callback function. The example request looks like this:
sendXMLDocument(smf_scripturl + "?action=reminder;sa="+ sa +";xml", "user="+user+"&sc=" + sc, onReminderReceived);
See the code before to see how I collect the form information.
But you ask how to tie this with the PHP part. Quite easy. You have to set the xml input variable. In this case SMF will load the Xml.template.php and won't output any layers.
In your action's function you set the $context['sub_template'] = 'mytemplate' and define in Xml.template.php the template_mytemplate() function. Look at other functions in there to see examples. Basically it's like normal templates but you write XML code instead of HTML.

That's all.

PS: for SMF specific examples look at the Stats.php. There is a very tiny, simple piece of ajax function. Search for

// Handle the XMLHttpRequest.
if (isset($_REQUEST['xml']))
{
// Collapsing stats only needs adjustments of the session variables.
if (!empty($_REQUEST['collapse']))
obExit(false);

$context['sub_template'] = 'stats';
getDailyStats("YEAR(date) = $year AND MONTH(date) = $month");
$context['monthly'][$year . sprintf('%02d', $month)]['date'] = array(
'month' => sprintf('%02d', $month),
'year' => $year,
);
return;
}

There is a template_stats function in XML.template.php for that output. There's also the js function to make the request and process the resulting xml data in Stats.template.php.
I will update all my mods in the next few weeks. Thanks for your patience.

SVG-Collapse (you need an SVG compliant browser)

Sarke

Thanks Rudolf, I'll look into that.

My MODs          Please don't PM me for support, post in the appropriate topic.

Advertisement: