HTML
  Home arrow HTML arrow Page 2 - Sending Email with AJAX: Developing the Cl...
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 
Sun Developer Network 
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? 
HTML

Sending Email with AJAX: Developing the Client-Side Application Layer
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 10
    2006-01-24

    Table of Contents:
  • Sending Email with AJAX: Developing the Client-Side Application Layer
  • Sending email with AJAX: defining the “email sender” module
  • Displaying contacts: defining the “contact listing” module
  • Adding new contacts: defining the “contact insertion” module
  • Initializing the AJAX email application: defining the “initializeEmailClient()” function

  • 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


    Sending Email with AJAX: Developing the Client-Side Application Layer - Sending email with AJAX: defining the “email sender” module


    (Page 2 of 5 )

    The first task involved in the creation of the pertinent client-side application layer rests in writing down all the JavaScript functions related to sending email messages and handling the responses received from the server. In this case, once a message has been submitted, the program will first fetch a PHP file called “sendmail.php”, which will take care of sending out the email, and then return to the client a specific response, whether the message has been sent successfully or not.

    As you can see, this process is extremely simple, since it’s very similar to submitting a regular online form. I’ll begin by defining the first JavaScript function, “getXMLHttpRequestObject()”, which comes in useful for returning XMLHttpRequest objects. Here is its definition:

    function getXMLHttpRequestObject(){
        var xmlobj;
        // check for existing requests
        if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4){
            xmlobj.abort();
        }
        try{
            // instantiate object for Mozilla, Nestcape, etc.
            xmlobj=new XMLHttpRequest();
        }
        catch(e){
            try{
                // instantiate object for Internet Explorer
                xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch(e){
                // Ajax is not supported by the browser
                xmlobj=null;
                return false;
            }
        }
        return xmlobj;
    }

    If XMLHttpRequest objects are pretty familiar to you, the above function shouldn’t be hard to understand at all. What it does, essentially, is return a new requester object each time it’s called up. Specifically, I’ve decided to code this function because I’ll use three different object across the application, which obviously will handle distinct tasks.

    Even though this may sound like a resource-consuming process, I have a good reason for doing this. The first requester object will take care of handling all the tasks related to sending email messages; the second one will tackle all the operations regarding the insertion of new contacts; and finally, the third object will handle the process for displaying them. Simple and sweet.

    Now, allow me to define another useful function, “sendEmailRequest()”, which is responsible for fetching the PHP file that actually sends email messages. Please take a look at its definition:

    function sendEmailRequest(){
        var message=document.getElementsByTagName('form')[1].elements
    ['message'].value;
        if(message.length>1000){message=message.substring(0,1000)};
        // open socket connection
        emailXMLHttpObj.open('POST','sendmail.php',true);
        // set form http header
        emailXMLHttpObj.setRequestHeader('Content-
    Type','application/x-www-form-urlencoded; charset=UTF-8');
        // get form values and send http request
        emailXMLHttpObj.send(getFormValues
    (document.getElementsByTagName('form')[1]));
        emailXMLHttpObj.onreadystatechange=emailStatusChecker;
    }

    As the above function illustrates, all the email messages will be submitted to the “sendmail.php” PHP file, by sending a POST request to the server, after the data entered into the corresponding form has been collected. In this case, the progress of the respective http request is checked by the “emailStatusChecker()” function, so I’d like to show you how it looks. Its signature is listed below:

    function emailStatusChecker(){
        // if mail request is completed
        if(emailXMLHttpObj.readyState==4){
            if(emailXMLHttpObj.status==200){
                // if status == 200 display server response
                displayServerResponse();
            }
            else{
                alert('Failed to get
    response :'+emailXMLHttpObj.statusText);
            }
        }
    }

    As you’ll agree, this function is pretty straightforward. In short, what it does is verify the status of the http POST request, after a message has been submitted (remember that email messages are handled by the “sendmail.php” PHP file). Once this file is fetched, the callback function “displayServerResponse()” is called in turn, which means that the process for sending email and receiving the respective server response is terminated by this new function. Thus, here is its short definition:

    function displayServerResponse(){
        var status=document.getElementsByTagName('h1')[1].firstChild;
        if(!status){return};
        // display messages by <h1> header
        status.data=emailXMLHttpObj.responseText; 
    }

    Indeed, the above function seems to be a very simple thing. However, it performs a pretty useful task, since it displays the corresponding server response after submitting a new message, whether this has been successful or not. Notice how this response is handled by the “responseText” property of the email requester object, and displayed by an <h1> header, which as you’ll recall, is placed on top of the message composing section.

    Well, the last function that you learned above completes what I called the “email sender” module of the AJAX application. So, the next step consists of defining the set of functions that compose the “contact listing” module, which takes care of displaying the list of contacts. Want to find out how these useful functions are defined? Right, let’s do it together.

    More HTML Articles
    More By Alejandro Gervasio


       · The second article of this series goes through the development of the required...
     

    HTML ARTICLES

    - Using a 3D HTML Table as a Recordset
    - Building a 3D HTML Table
    - Maximizing and Restoring HTML Images: Layer ...
    - Completing Construction of a Database Form w...
    - Maximizing and Restoring Images in a Tabular...
    - Building the Recordset for an HTML Database ...
    - Laying Out a Database Form with HTML
    - Tabular Database Form Functions with HTML
    - Tabular Database Forms with HTML
    - Using the Find Functions for HTML Database F...
    - Sorting for Database Forms with HTML
    - Edit and Other Database Form Functions with ...
    - More Database Form Functions with HTML
    - Database Form Functions with HTML
    - Using the HTML Table Element as a Recordset






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT