Creating Ajax form in Cakephp (with Prototype.js)

(If you are using jQuery with cakephp you may want to view this article, Creating Ajax form in Cakephp using jQuery )

With the help of prototype/JavaScript we can build really cool and fully functional ajax form in CakePHP really fast.

I have been using the ajax forms in CakePHP for a long time now but in the view of continuous development work always on at CakePHP framework development community i have had to alter ways to write the same code. So here is the latest one.

Prior to Cakephp 1.2 i had been using

to create a ajax form request element but when i tried to update my old site to latest 1.3 version of cakephp it would not work anymore. The latest way to create a ajax form element had turned to something like this:

So i had to replace my /cake/ directory to update to the latest version but again i had to do changes (very few and fast though) to make my old ajax forms working with latest version. At the last i came up with this post which shows how to create ajax form in the latest version 1.3.

In this example we will learn to search a database table using ajax form and then display results right under the form itself. We will display “results found” or “results not found” titles in the result area after the request is complete. To customize results, you will need tp edit controller action or corresponding view code yourself.

A loader, while the ajax request is in process will be shown and eventually hidden when the request is complete and results are ready to display.

Let’s start building it. We assume that we have an existing action “index” in our CharityController and it has a corresponding view “index.ctp” within which we place our ajax form. This form will be submitted to a different action “search_retailers” of the same Charitycontroller and results will be shown just under the ajax form in the “index.ctp” view through ajax.

Okay. The following line of code will generate a <form> tag with a few attributes and a corresponding javascript code right under the form tag. Following the complete php code which we place in our index.ctp view (you will need to call prototype.js library inside your layout or header view file to make ajax work):

The php code above generates somewhat similar html + javascript code as under when loaded into the browser. I say similar because it will generate random form id each time you load the page (form2064479612 here). Here is the html output:

Although the code above is quite self explainatory i will explain the main function $ajax->form() used in the view in detail.

The $ajax->form() defined in /cake/libs/view/helpers/ajax.php can accept all option within a single argument in array format. The first element ‘type’ of the passed array defines ‘method’ of the form and if omitted it takes POST by default. You can define all additional and needed options within second ‘options’ element. Breaking ‘options’ array elements further, it accepts “model” which is the name of the model, not important though. The ‘update’ element is the id of HTML element, normally a DIV element which will be populated with results returned by the ajax request. The ‘url’ can carry form action url as a string or in the form of an array which again has the controller and action name within itself. In this example, we will pass an array as url which has controller and action names as “Charity” and “search_retailers” respectively. Next element of the ‘options’ array is the ‘loading’ which can carry JavaScript code to be run while the request is in process. In the example, we will show a spinning animated image wrapped within “plsLoaderID” DIV element with the help of method of prototype library. The last element of ‘options’ array here is ‘loaded’ element which also contain a piece of JavaScript to be run just after the request has finished loading the data in buffers. In this example, we will hide that spinning animated image container “plsLoaderID” with Element.hide() method of prototype when the request has finished loading the data. That’s all explaining the parameters of $ajax->form() method.

Now we create a “search_retailer” function in our CharityController which processes the request submitted through our form. Here is the example of this function:

Now, as the stores data is stored in $stores variable we will create a new view named “search_retailer.ctp” inside views/charity/ directory to display the data. The final html output by our “search_retailer.ctp” view will be shown within our ‘update’ id which is “retailersListID” here in this example. Below is the example code written inside search_retailer.ctp view:

Once all things are in place and “List Retailers” button is pushed you should see a printed list of record or just a message “Sorry! no such store was found!” with a “Results not found” title.

I hope this example gives you enough idea about how to create an ajax form with the help of prototype within cakephp. Your comments are appreciated.

2 thoughts on “Creating Ajax form in Cakephp (with Prototype.js)

Leave a Reply

Your email address will not be published. Required fields are marked *