XML
  Home arrow XML arrow Page 3 - Building an AJAX-Based Chat: Coding the Re...
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: Coding the Receiver Module
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 15
    2005-11-15

    Table of Contents:
  • Building an AJAX-Based Chat: Coding the Receiver Module
  • Retrieving messages from the database: defining the “getChatData()” function
  • Building the chat layout: writing the “createMessageBoard()” and “createMessageBox()” functions
  • Registering chat users: defining a simple login page
  • Putting the pieces together: building the whole chat page

  • 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: Coding the Receiver Module - Building the chat layout: writing the “createMessageBoard()” and “createMessageBox()” functions


    (Page 3 of 5 )

    Since I want the chat to be developed entirely with JavaScript (in conjunction with the proper PHP files), I’ll define a couple of additional functions, which will create a basic page layout. As you’ll see, the chat page will comprise two sections: a top section, useful for displaying the whole list of messages, and a bottom section, where the user can submit new comments by using a typical web form.

    Having explained how the chat page will be laid out, here is the source code for the first function, “createMessageBoard()”, which creates the containing <div> element, handy for displaying the list of messages:

    function createMessageBoard(){
        var mdiv=document.createElement('div');
        mdiv.setAttribute('id','messages');
        document.getElementsByTagName('body')
    [0].appendChild(mdiv);
    }

    Definitely, the above function isn’t rocket science. It simply creates the corresponding <div> element, then assigns to it an ID, and finally appends it to the document tree.

    Now, let’s get rid of rather irrelevant code and see how the “createMessageBox()” function looks:

    function createMessageBox(){
        // create message box container
        var mdiv=document.createElement('div');
        mdiv.setAttribute('id','messagebox');
        // create message form
        var mform=document.createElement('form');
        // create message box
        var mbox=document.createElement('input');
        mbox.setAttribute('type','text');
        mbox.setAttribute('name','message');
        mbox.className='msgfield';
        // create 'send' button
        var mbutton=document.createElement
    ('input');
        mbutton.setAttribute('type','button');
        mbutton.setAttribute('value','Send');
        mbutton.onclick=sendMessage;
        // create login text
        var sp=document.createElement('span');
        sp.appendChild(document.createTextNode
    ('Logged in as: <?php echo $user?>'));
        // append elements
        mform.appendChild(mbox);
        mform.appendChild(mbutton);
        mform.appendChild(sp);
        mdiv.appendChild(mform);
        document.getElementsByTagName('body')
    [0].appendChild(mdiv);
        mbox.focus();
        mbox.onfocus=function(){this.value='';}
    }

    As shown in the above function, the bottom section of the chat page is generated by building up dynamically a few (X)HTML elements. First, the function creates a general containing <div>, which will house the appropriate web form for sending out new messages. Then, it generates the form itself, by including a single text input box and a form button, and finally inserts the whole structure into the document tree.

    Additionally, there are a couple of things worth being analyzed in detail. Notice how a <span> element is created, in order to display the nickname of the current user. With reference to this, the following lines:

    var sp=document.createElement('span');
    sp.appendChild(document.createTextNode('Logged
    in as: <?php echo $user?>'));

    will display the user’s nickname, usually registered on a previous login page. However, you should not worry about this for now. In a moment,  I’ll demonstrate how the login page works. Now, by returning to the code responsible for displaying the corresponding nickname, the below embedded PHP statement:

    <?php echo $user?>

    will do the trick. For example, if a user has logged into the chat by using “JohnD” as the nickname, then the legend “Logged in as: JohnD” will be displayed to the right of the text input box. As you can see, the process is fairly understandable.

    At this stage, I provided you with all the JavaScript functions that handle and manipulate the “receiver” request object, as well as the code responsible for building a rough layout for the chat page. Given that, the next step involved in coding the chat application will be writing a simple login page, which registers a visitor’s nickname and then redirects the user to the appropriate chat page.

    More XML Articles
    More By Alejandro Gervasio


       · Over this second article, the "receiver" module of the chat application is properly...
       · This is my first foray into AJAX and DOM, and I this tut was the best place to...
       · Thank you for your compliments on my AJAX tutorial. I'm really glad to know that...
       · Nice articleIsnt it bad pratice to call a function recursively without ever exiting...
       · Thank you for commenting on my article. Comming to your question, recursion can be...
       · Nice article,It would be great if they had a link to an example at the beginning...
       · Thank you for the kind comments on my AJAX tutorial. Of course, when applicable,...
       · Nice article & very interesting, since I have been looking for a thorough, detailed...
       · Hello Rasmus,I'm glad to know that my AJAX chat was what you were looking for....
       · I loved your article. It helped me understand AJAX more. BUT, this does not want...
       · Thank you so much for the kind comments on this tutorial. It's good to see it's been...
       · Nice article. I had just finished my own AJAX chat tutorial when I ran across yours....
       · I'd like to thank you for commenting on my AJAX-based chat tutorial, particulary if...
     

    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 3 Hosted by Hostway
    Stay green...Green IT