JavaScript
  Home arrow JavaScript arrow Page 2 - Developing the Client-Side Code for Server...
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? 
JAVASCRIPT

Developing the Client-Side Code for Server-Side Data Validation with AJAX
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2006-07-18

    Table of Contents:
  • Developing the Client-Side Code for Server-Side Data Validation with AJAX
  • Triggering HTTP requests in the background: meet the world of requester objects
  • Processing server responses: defining the “displayErrorMessage()” function
  • Controlling the flow of data validation: defining the “initializeForm()” function
  • Integrating the client-side application layer: listing all of the JavaScript functions

  • 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


    Developing the Client-Side Code for Server-Side Data Validation with AJAX - Triggering HTTP requests in the background: meet the world of requester objects


    (Page 2 of 5 )

    In order to start developing all the JavaScript functions that integrate this particular application, I first must work with requester objects. In this way I will provide the data checking system with the ability to perform HTTP requests without having to reload the web page, and to call the corresponding input validation routines on the server.

    To achieve all the tasks that I described above, here is the definition of the “sendHttpRequest()” function. As the name suggests, it is responsible for triggering HTTP requests silently. Please examine the code listed below:

      function sendHttpRequest(url,callbackFunc,respXml){
       var xmlobj=null;
       try{
            xmlobj=new XMLHttpRequest();
        }
        catch(e){
            try{
                xmlobj=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                alert('AJAX is not supported by your browser!');
                return false;
            }
       }
       xmlobj.onreadystatechange=function(){
            if(xmlobj.readyState==4){
                if(xmlobj.status==200){
                    respXml?eval
    (callbackFunc+'(xmlobj.responseXML)'):eval
    (callbackFunc+'(xmlobj.responseText)');
                }
            }
        }
        // open socket connection
        xmlobj.open('GET',url,true);
        // send http header
        xmlobj.setRequestHeader('Content-Type','text/html;
    charset=UTF-8');
        // send http request
        xmlobj.send(null);
      }

    This is pretty simple. As you can see, the function listed above takes three parameters to do its business: first, the URL to which the request will be made, then the name of the callback function that will process the response sent back by the server, and finally a Boolean flag indicating whether the corresponding server response will be expected as plain text (that is, using the “responseText” property), or as XML (utilizing the “responseXML” property).

    To finish describing the previous function, let me tell you that it will also create an XMLHttpRequest object in a cross-browser fashion, and send out all the requests via the GET method. Short and simple, isn’t it?

    Well, now that I have a requester object to work with, the next logical step consists of defining the respective callback function. This function processes all the responses sent by the server, whether a particular value entered in the form field was considered valid or not. Are you starting to see how this AJAX-based data validating application will work? I hope you are!

    Over the next few lines, I’ll be defining the brand new “displayErrorMessage()” JavaScript function, which will handle the respective server responses after calling a specific PHP validation routine. Click on the link below and keep reading.

    More JavaScript Articles
    More By Alejandro Gervasio


       · In this second article of the series, all the JavaScript functions that compose the...
       · where to find the third part of Server-Side Data Validation with AJAX? I cann't find...
       · Thank you for your interest on my AJAX article. You can read the last part...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






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