Today I’ve created simple AJAX-enabled plugins for Smarty. I don’t try to develop powerful reach-applications framework. I can give you only idea how to integrate AJAX-technology into Smarty. But if you have any offers how to improve anything I’ve described or if you just want to leave feedback please post you comments on my site.
In my practice I need several things from AJAX: to update some nodes in DOM, to send forms without post-back, to retrieve some values or to perform server-side calculation (maybe using database or other server-side resources). It’s necessary to write tons of JavaScript to implement my requirements in spite of using cool JavaScript library Prototype.
I decided to integrate Smarty with AJAX. There are three Smarty plugins has been created: ajax_update, ajax_call, ajax_form. Below all this plugins will be described.
ajax_update
This Smarty function can be used for update some parts of web-page.
Sample usage:
1 2 | <a href="#" onclick="{ajax_update update_id='intro_content' function='update_intro' params='page=about'}">About</a> |
Possible parameters:
- url – URL for AJAX-query (when no URL was specified current one will be used)
- method – query method (by default get, can be get or post)
- update_id – ID of HTML element for update
- function – function which will be called
- params – URL-encoded parameters
ajax_call
This Smarty function can be used for call PHP function on server side and retrieve its output.
Sample usage:
1 2 | <a href="#" onclick="{ajax_call function='calculate' params_func='calc_params' callback='calc_cb'}">Calculate</a> |
Possible parameters:
- url – URL for AJAX-query (when no URL was specified current one will be used)
- method – query method (by default get, can be get or post)
- function – function which will be called
- params – URL-encoded parameters
- callback – JavaScript function which will be called when query will be completed
- params_func – JavaScript function used when you need custom parameters calculated on client side
ajax_form
This Smarty block can be used for submit Web-forms without post-back.
Sample usage:
1 2 3 | {ajax_form method="post" id="form_register"} Any form-element can be placed here {/ajax_form} |
Possible parameters:
- url – URL for AJAX-query (when no URL was specified current one will be used)
- method – query method (by default get, can be get or post)
- params – URL-encoded parameters
- id – form ID
- callback – JavaScript function which will be called when query will be completed
Samples
These plugins are quite simple and I think everyone can create even better than mine. I’ve just wanted to show you idea how integration can be done. Working examples can be found here. Also you can download full sources.

[...] In my previous post I’ve described several simple AJAX plugins. Now I’ll show how to use one of them — ajax_form — in real applications. I think this is the most powerful plugin therefor I’ll dwell on it more elaborately. [...]
Я еще не успел полностью ознакомиться с данной технологией, но она уже мне нравится по просмотренным примерам. Будем изучать.
Hey very cool! keep up the good work!
really a great idea!
Попробовала использовать вышепредложенные плагины. У меня довольно быстро все получилось, хоть до этого с Ajax я была знакома только в теории.
Пара замечаний:
С нетерпением жду ajax_table :) Двигаешься в правильном направлении, так держать :)
hey great job… just one thing… when used in IE, the page gets cached and does not refresh with new content. anyway to solve that?
To prevent IE from caching pages you can add to all url any random string (for example http://somehost.com/index.php?4397593847)
You can do this using JavaScript-function Math.rand() or PHP function mt_rand() or rand().
Another way is to set page headers to disable cache:
2
3
4
5
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
hi,
I have one question:
can i use/call class functions in the ajax_update and ajax_call?
thx
Unfortunately, this is not possible in current version.
I don’t speak inglish, but let me say. Your plugin es really fantastic
[...] AJAX-enabled Smarty plugins (tags: ajax smarty webdev) [...]
клевый ход , так держать
how can smarty section implemented using ajax.
i tryed but got some error.
please help
Hmm. I haven’t understood your problem. Can you provide code that you use?
[...] Bellow is the simple resources of Ajax Enabled Smarty plugins. I got from this site [http://kpumuk.info/ajax/ajax-enabled-smarty-plugins/]. this part of articles In my practice I need several things from AJAX: to update some nodes in DOM, to send forms without post-back, to retrieve some values or to perform server-side calculation (maybe using database or other server-side resources). It’s necessary to write tons of JavaScript to implement my requirements in spite of using cool JavaScript library Prototype. [...]
[...] AJAX-enabled Smarty plugins [...]
This website is Great! I will recommend you to all my friends. I found so much useful things here. Thank you.
hi
its greate job … well i am new to smarty and facing some problem, please help me as i am already using the smarty, now i dont want to change existing smarty setup, without changing the existing smarty configuration and i want to intigrate your AJAX-enabled Smarty plugins in my existing smarty configuration. so can u help me how do i intigrate ur plugin in it??? and as i tryed to intigrate i get syntax error called “unrecognized tag ‘ajax_form’” so pls help me in it.
let me know the steps..
thanx in advance n awaiting for ur reply
regards
-skonealone
hi once again .. as i intigrated the smarty_ajax plugin but still me not able to post the variables to the action page..
when i click on submit button .. nothing is happening..
pls let me know where i am doing wrong..
regards
-skonealone
hi kpumuk,
how can i realize this: (look at the params, need this in smarty foreach)
thx
Hello, mr. ajax
This is standard Smarty future: you can use $variable inside {} in following way:
hello kpumuk,
great work, thank you very much.
i found the callback isn’t working when using ajax_call.
can’t figure out why.
wouldn’t it be better to merge call and update?
at least, ajax_call with a callback to “update a div” would do it.
regards,
gotmyredhaton
Ajax without XML????? I can’t understand, I need XML…
Hi, congratulations for this plugun, its fantastic. But, how i can auto-load update intro in a div element?? onLoad event cannot load update_intro, only onclick is possible!!! Thanks
Hi, I want to use update to do that on some part, process a code and show the same smarty templated thing every 1 second.
Should pasting a line like {ajax_update update_id="intro_content"
function="update_intro" params="page=about"} inside the javascript function that trigger timers work? Will that make it to recalculate the smarty templated code i wan to show or it’s already locked?
Hi Franco!
Just look at the examples. In chat.php and corresponding Smarty template you can find periodically updated part of page. Think it can help you.
thank you very much, you were very helpfull
Szenna’s comment inspired me to modify smarty_ajax to support calling object methods from ajax_call and ajax_update. I added an optional parameter “object” to both so you can call the function as a method of an object.
If you need this functionality, you can get it from http://logankoester.com/smarty_ajax/
@Kpumuk: Are you interested in incorporating my patch in the next release of smarty_ajax?
Hi, nice very nice page..!
Good luck !
Отличные плагины!
Спасибо тебе! Был бы очень рад увидеть еще плагины для смарти…
Это то, чего давно не хватало смарти.