XML
  Home arrow XML arrow Building an AJAX-Based Chat: Interacting W...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Building an AJAX-Based Chat: Interacting With a Database
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 35
    2005-11-21

    Table of Contents:
  • Building an AJAX-Based Chat: Interacting With a Database
  • Adding messages to the database: looking at the “sendchatdata.php” file
  • Reading messages from the server: defining the “getchatdata.php” file
  • Putting the files together: the complete chat application at a glance

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building an AJAX-Based Chat: Interacting With a Database


    (Page 1 of 4 )

    In this third and final part of a tutorial that explains how to build an AJAX-based chat application, Alejandro Gervasio covers creating the PHP files that directly interact with a MySQL database, adding and fetching chat messages. He also makes some suggestions for expanding the functions of the application.
    A downloadable file for this article is available here.

     Here we are again. This is the concluding part of the series “Building an AJAX-based chat.” Welcome back. As you probably remember, developing an extensible chat application by using AJAX is not only a fairly easy task, but it’s also a fun experience. Stripped down to its bare bones, I implemented the driving logic of the chat by splitting the whole application into two different pieces of code. These are the “sender” and “receiver” modules respectively, which utilized two requester objects in order to handle independently http requests, for inserting new comments into the database and fetching chat messages.

    Stepping back briefly to the previous part of the series, I coded all the JavaScript functions that made up the “receiver” module, as well as some additional functions aimed at building the basic layout of the chat page. Also, in order to illustrate with a clear example how the chat application was capable of registering user’s nicknames, I went through the development of a basic login page. This page implemented a simple PHP session mechanism for storing nicknames during the complete chat session.

    Considering that the AJAX-based program is near to completion, the question is: what is next? Remember that from the very beginning of this series, I paid strong attention to writing all the code that runs on the browser, oriented to working with requester's objects, along with some simple PHP snippets. However, as you may have noticed, I didn’t get my hands into the complexities of creating the PHP files that directly interact with a MySQL database, handy for adding and fetching chat messages. Since I purposely left “untouched” the server PHP scripts for last, in this final article, I’ll code the required PHP files for accessing the database and handling messages.

    Before I leap into writing down the PHP code, a brief clarification is in order: I’ll use a MySQL wrapping class for accessing the mentioned database, so all the sample files will use this class for running SQL queries and handling result sets. However, as you’ll see shortly, the code is flexible enough to be changed to work with a procedural method, in case you’re not familiar with an object-oriented approximation.

    Having drawn the guidelines for this final tutorial, let’s get rid of the irrelevant details and start writing the server code for the chat application. Let’s go!

    More XML Articles
    More By Alejandro Gervasio


       · This last article of the series goes through the development of the PHP scripts...
       · Great article, thanks!
       · Thank you for the compliments on my AJAX article.Regards.
       · Hi Alejandro,again a really interesting article. Thanks a lot!One question...
       · Hello again,Thank you for the positive comments on my article. Comming to your...
       · Thanks for your quick reply Alejandro. And good to get your confirmation that it...
       · Hello Matthijs,I'd like to thank you for reading the tutorial. Good...
       · Hi Alejandro! Thank you very much for your great AJAX chat tutorial. It has helped...
       · I solved it! I just added to the sendMessage function:...
       · Fantastic article you've really helped me to understand.But I have a question...
       · As posted earlier by someone else, when pressing return on a message, it does not...
       · Thanks a lot for your comments on the article. Also, I'm glad to know that you found...
       · Hello,Thanks for reading my article. Regarding your question, that happens...
       · Thank you for your comments. With reference to your question, you should modify the...
       · Create a new function:function submitSend(){ ...
       · Thank you for submitting your feedback on this article. Yeap, creating a function...
       · the article by Alejandro Gervasio is really brilliant, it worked first time, and it...
       · Thank you for your compliments on my AJAX article, since they're very welcome. I'm...
       · Your tip worked for me in Firefox. But in IE I still couldn't use the enter key. I...
       · Thank you for your feedback. Also, it's good to hear that my suggestion worked for...
       · Hello!First of all, congratulations for the great article. It really helped me...
       · Hello Lucas,Thank you for your kind comments on my article. I've tested the chat...
       · Hi guys!I created a control that works like an AJAX container. Basically the way it...
       · Thanks a lot for posting your comments here. Even when I'm not well versed on...
       · Thank you very much for your explanation. I wouldn't say that I understand...
       · Hi again,Sorry to hear that the http headers didn't solve your problem. However...
       · hey sorry for the inconvencience...but after a while I got the chatfunction to...
       · Hello,Good to know that the http headers now worked for you. Concerning your...
       · Hello, Sorry it's me again.I'm working on a online/offline function. I have one...
       · Hello again,It's good to see the way that you redefined the structure of the...
       · Well that's the problem.. if I for instance would use this code:function...
       · Hello again,I see you're still in problems, but there's one thing that came up...
       · Well thanks for you help.. And your idea is a good one.. But i figured out what...
       · Hi again,I'm glad to know you figured out how to solve the problem. Good luck...
       · I have been trying to implement smilies, but whenever i convert certain char strings...
       · Thanks for the comment on my AJAX article. Concerning your question, you should...
       · Thanks! it worked great.However after implementing this code the name no longer...
       · I'm glad to know the suggestions I made previously worked for you concerning the...
       · strangely enough the...
       · Hello,Please check the above post, in order to get an idea on how to include...
       · Thank you for your well organized and refreshing explanation of a simple Ajax...
       · Hello Bob,First off, I’d like to thank you for your kind comments on my AJAX...
       · Found the article fantastic. Very insightful. Was wondering if one could easily...
       · Thanks for the kind words on my Ajax article. Regarding your question, a current...
       · Hi,thanks alot for the time and effort you are putting in all this its really...
       · Hi there in Uganda,It’s good to know you liked my article on the Ajax chat. Now,...
       · Hi,thamks alot for the direction,i did try to do it,problem is its just not working...
       · Thanks again for the comments. Like I posted before, implementing private messages...
       · Hi,thanks for the patience,i truly appreciate you spending your time on this,my...
     

    XML ARTICLES

    - Using Regions with XSL Formatting Objects
    - Using XSL Formatting Objects
    - More Schematron Features
    - Schematron Patterns and Validation
    - Using Schematron
    - Datatypes and More in RELAX NG
    - Providing Options in RELAX NG
    - An Introduction to RELAX NG
    - Path, Predicates, and XQuery
    - Using Predicates with XQuery
    - Navigating Input Documents Using Paths
    - XML Basics
    - Introduction to XPath
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek