Updated: 2010-02-23, published: 2006-07-26.
Article shows how to create Ajax forms with server-side actions using the Dojo toolkit and Zend Framework. It guides you through creation of a sample user login form that uses dojo.xhrPost to sent the login request and Zend MVC to respond on this request.
So I tried this new way and created a simple form with the Dojo and Zend Framework. The result is just excellent for me and I wish Dojo toolkit growing and hope that it will acquire a few more complex components, like DataGrid with paging and sorting. And here is a short review of the creation.
The content of the article can be interested to you even if you only read it. But if you want to improve your skills and create the similar Ajax page, you need to get the Dojo toolkit and Zend Framework. Installation of the Dojo toolkit is very simple: just unpack it to the folder that is accessible by site users. The next you need to download an installation of Zend Framework. Use the installation instruction to choose a best place to unpack the archives. As for me, I prefer "/libs/dojo/" for Dojo and "/libs/server/ZendFramework/" for Zend Framework.
A typical login form contains a server-side login action, a form with Login and Password fields and Login button that executes server login action. The login action returns the status of operation or redirect URL. It was exactly what I wanted to create for my system.
I created a page that hosts the login form with the controls, which I wrote about a few lines above, and includes dojo toolkit packages. The page itself is a PHP script intended to be run in a Zend_View context.
The following Admin Contoller has two actions: for displaying the Login page and executing Login action.
The files above are a framework of the future page, which will be extended with the right code. They demonstrate a page structure and focus your attention on the page construction, without implementation details. Almost any form contains similar construction blocks: inputs, buttons for submitting data to a server, server actions.
On the next step, I need to extend the functionality and replace the comments with the right code. The client code should send request to the server and, when server returns a URL, change the location. Otherwise it shows server's response assuming that the server returned an error message. The request could be sent with the dojo.xhrPost method, you can find a detailed explanation of the dojo.xhrPost in the manual. So, the code of the login client function can be similar to the following:
Server side action should validate the passed login and password, check if a user with such credentials exists and return the "url:/Login/Success/" when login is successful, or error message when it is not. The following code example contains very simple check of login/password and login - definitely you need to change this part when you consider using similar code in your application.
You can try to login with username "joe" and password "secret" or try to submit some invalid inputs to check how the system reacts to the wrong data.