Using AJAX in PRADO Application with CallBack Event

Posted by NanoTutor | Thursday, April 02, 2009 | | 0 comments »

Requirements : You have already know about prado v3.x basics, and Connection to Postgre SQL database using TActiveRecord.
This time I will show you how easy using Ajax in Prado for create a simple web application with two combo box that have related each other, and both are connected to database for binding the items. When I choose the item from the first combo box , the second combo box will load items that have a relation with item that I have choosed from the first combo box. In desktop application there was no problem for doing that, but in web application you can’t directly do that, because normally in web if you want to access to the database you must send some parameter and then the page and item that you have choosed from the first combo box will automatically refreshed.
The First Question is : “how can I do that things, without refreshing the page and the selected item still appear at the first combo box”? The answer is , You must using Ajax.
The Second Question Appear: “I don’t know anything about Ajax programming, Can I do that using Prado ?” The answer is , Of course,You don’t have to know about Ajax programming, the only thing you know is Prado already support Ajax, and you can easily using Ajax with Prado.
This is the example program :
We have 2 table for example, each table will represent the combo box at the presentation logic. Then I’ll create database in postgre sql 8.x named “dbblog”



This is the sample data :


Of course we must already prepare the prado project skeleton :

This is the code for Application.XML . there are two important setting here the “ActiveControls” and “Database Connection to PostgreSQL with TActiveRecord”

Next, we must create two class that represent 2 table (tbl_prov.php & tbl_subprov.php) which inherited From TActiveRecord Class. Place it below database folder.
tbl_prov.php

tbl_subprov.php

Now we must create 2 files below pages folder : Home.page (presentation) and Home.php (process)
Home.page

Home.php

That’s all,.. now you can try to access your application. Then you can select any item in the first combo box, and the second combo box will automatically load items related to it. And of course without refreshing the page.. it’s so easy using Ajax with Prado Framework..And the most important, there are no different logic between web application with Prado Framework and desktop application (event driven) you can see it at home.php file and compare it with logic when you create a same applicaton with vb or other even driven desktop application.

0 comments