.
| home / programming / javascript / kh / 1 | [previous] |
|
|
You may have noticed that all Ajax requests we've made are to a file called connector.php. This file creates a connection between the Ajax engine and a server-side object called Post.
In order to respond to the request with valid XML we use a header that sets the content-type of the file to XML. We then retrieve the query variables and make a custom method call with the method, id, title and description values from the query string. Let's take a look at the methods that we'll use in the Post object.
We'll first start by creating the database.
The constructor function in our Post class requires a file called mysql_connect.php.
This file needs to contain all of the information to connect to your database and should be placed in a secure location on the server, preferably in a directory not accessible from the Web.
With this file in place we can create the methods to get, save and delete posts. The save and delete methods are fairly straightforward, while the get method is a little more involved. This method retrieves the results from the database ordered by id and while iterating them it constructs an XML file from the values, which is then returned to the connector.php file as a response to the Ajax request. The other methods that are contained in the source download save, update and delete posts and then trigger the get method to gather all of the results and return them as XML.
Now that we've created a request model for retrieving data from a database it's important to create a security model for the requests. One way to accomplish this is to create a cookie based security model which is only accessible on the server-side.
Kris Hadlock is the co-founder of 33Inc alongside Robert Hoekman. He is the author of Ajax for Web Application Developers and has been a feature writer for numerous Web sites and design magazines. To learn more about Kris, visit his Web site at www.krishadlock.com or his blog at www.krishadlock.com/blog
| home / programming / javascript / kh / 1 | [previous] |
Created: March 27, 2003
Revised: Sept 1, 2006
URL: http://webreference.com/programming/javascript/kh/1