. Database-enabled Ajax with PHP - WebReference.com-

spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / kh / 1 To page 1current page
[previous]

Technical Lead
Thomson Reuters (Markets) LLC
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


Database-enabled Ajax with PHP

Bridging the Gap

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.

The Database Connection

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.

Conclusion

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.

About the Author

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 To page 1current page
[previous]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

Created: March 27, 2003
Revised: Sept 1, 2006

URL: http://webreference.com/programming/javascript/kh/1