View Full Version : XMLHttpRequest and Addons
rmoss
05-26-2010, 09:37 AM
Hiya folks,
creating an addon currently that needs information requested using Ajax to GET information from the server, I've created a function in the util.php file for my addon but cannot seem to get it working.
Can anyone give me a tip on how to reference that function, my Ajax code seems to be fine it is actually doing something but its not calling the function like it should be.
Appreciate the help.
jonyo
05-26-2010, 11:44 AM
Hiya folks,
creating an addon currently that needs information requested using Ajax to GET information from the server, I've created a function in the util.php file for my addon but cannot seem to get it working.
Can anyone give me a tip on how to reference that function, my Ajax code seems to be fine it is actually doing something but its not calling the function like it should be.
Appreciate the help.
What URL are you trying to use to make the AJAX call?
If you use AJAX.php in main folder of software:
In addon, create a file CLASSES.ajax.php (or ADMIN.ajax.php for ajax calls in admin panel).
In that file, put something like this:
<?php
//my_addon/CLASSES.ajax.php
// DON'T FORGET THIS
if( class_exists( 'classes_AJAX' ) or die());
class addon_my_addon_CLASSES_ajax extends classes_AJAX {
public function my_task ()
{
if (!$this->_checkSession()) {
return;
}
echo 'Some Data!';
return;
//example of how you would call function in util
$util = geoAddon::getUtil('my_addon');
$util->myUtilFunction();
}
private function _checkSession ()
{
//If any security checks were needed, such as making sure user is logged in, do them here.
//return false if not authorized, or return true to allow
return true;
}
}
Now, in a template on one of the pages in the software, in the <body>...</body> section, you would add:
<script type="text/javascript">
//<![CDATA[
//{literal}
Event.observe(window, 'load', function () {
new Ajax.Updater('insertDataBox', 'AJAX.php?controller=addon_my_addon&action=myTask',{
evalScripts : true
});
});
//{/literal}
//]]>
</script>
<strong>Data Returned:</strong>
<div id="insertDataBox"></div>
Of course the <script>... part you could put in the <head> section, just using the above as a simple example to get you started.
In all of the sample code above, you would replace "my_addon" with the name of your addon's folder.
Note that Prototype JS is called on every page in the software, so you can use it to make AJAX calls a lot easier than having to "roll your own" way. In case you aren't familiar with Prototype AJAX calls:
Prototype JS docs (http://prototypejs.org/api/ajax)
rmoss
05-26-2010, 01:05 PM
Hi Jonyo,
Right i noticed the problem with my code,, do i pass variables to the util function like i would any other ?
My url used within the addon form is {$classifieds_file_name}?a=ap&addon={addon_id}&pag e=main
Its a select/option list im creating which updates other information when an item is selected, its the requesting of data thats been the issue.
jonyo
05-26-2010, 03:48 PM
Hi Jonyo,
Right i noticed the problem with my code,, do i pass variables to the util function like i would any other ?
My url used within the addon form is {$classifieds_file_name}?a=ap&addon={addon_id}&pag e=main
Its a select/option list im creating which updates other information when an item is selected, its the requesting of data thats been the issue.
You can certainly do it that way, but having a page dedicated to an ajax call might be a little more than you need, unless you need to be able to have the user assign a template to the page and stuff like that.
For ajax calls, I would recommend to make the call to AJAX.php, like it does in that sample code I posted above. It takes in "controller" to tell it what file to call up, and "action" to tell it what method to call in that file (so you can have one file and class that is in charge of a bunch of different ajax calls).
To pass parameters, just send them in the URL and access them via the GET vars. As far as how you get the data back, if you are using the javascript Ajax class in Prototype it's pretty easy. Otherwise if you are using your own method to make AJAX call you would have to handle that yourself.
rmoss
05-27-2010, 08:09 AM
Thanks for the help Jonyo, got it working perfectly now.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.