AJAX parts in plugs AJAX parts in plugs

HOWTO make AJAX-part in your plugin

In Seditio if you call plug.php?r=yourplug that would call a file from plugins/code/yourplug.php as a standalone without header/footer. In Cotonti there is no such thing but it is replaced with so-called AJAX plugin parts.

AJAX-mode parts of plugins are standalone, without header/footer. They should use hook "ajax" and be called this way: plug.php?r=yourplug which will call plugins/yourplug/yourplug.php or plugins/yourplug/yourplug.ajax.php or whatever you name it.

So, to create an AJAX part in your plugin you should:
  1. Create a part of the plugin, e.g. my_plug.ajax.php (could be any other name as well).
  2. Set its Hook=ajax.
  3. Code it just as a normal plugin, keeping in mind that it has not called header.php and footer.php. You can also set HTTP headers there with header() function before you send any output.
  4. Use it this way: plug.php?r=my_plug

Then you can call that part from other parts. Here is example jQuery code:
$.get('plug.php?r=my_plugin', { id: 123, name: 'Peter' },
    alert("Data Loaded: " + data);

Note that when submitting forms with GET (sometimes) or POST (always) you need to supply it with anti-XSS parameter x. Here are PHP examples for GET and POST:
$t->assign('MY_JS_FUNC', "$.get('plug.php?r=my_plugin', { id: $id, x: '" . sed_sourcekey() . "' },
    alert('Data Loaded: ' + data);
$t->assign('MY_JS_FUNC', "$.post('plug.php?r=my_plugin', { a: 'send', name: 'Peter', x: '" . sed_sourcekey() . "' },
    alert('Data Loaded: ' + data);

If you need to submit a form from your document via AJAX you can use ajaxSend() function which will assemble it automatically:
url: 'plug.php?r=my_plugin',
method: 'POST',
formId: 'myformid'

1. urlkiller  2009-03-20 12:21
is there any actual working example here at the docu?
2. tensh  2009-04-30 16:20
Tutorials through examples are the best thing. People who create in Seditio on daily basis are so much into the code that they assume what they write is easy to understand. In fact it isn't.

A working example would be good.
3. Elgan  2009-05-26 21:19
nice documentation. Works well :)

ill have that example for you peeps in a minute.
4. tensh  2009-06-30 21:34
Elgan, can you show this example? :)
5. ez  2009-08-02 04:57
Are there standard items now that work with AJAX.
What I mean is, can I get the content of:
Polls,Online,tagclous, news, new in forums (stuff that are in the index)
on a AJAX way...

I want to hide stuff from the index, and get it with AJAX when the user needs it... (ondemand)

hope to hear from someone
6. urlkiller  2010-04-09 20:04
one year later ... no example
7. ven7ura  2010-05-06 06:49
We need an example. I have been working on trying to apply AJAX on one of my plugins, but it seems that the MarkitUp plugin is not solid (does not use all features) enough to put it as an example.
8. Hodges  2013-04-21 16:46

*bump* ?

I want to start learning how to incorporate ajax elements into plugins.

Only registered users can post new comments