JavaScript
  Home arrow JavaScript arrow Page 3 - 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 - Processing server responses: defining the “displayErrorMessage()” function


    (Page 3 of 5 )

    As I explained right at the beginning of this series, real data validation will be performed on the server. Period. However, once a particular form field has been checked, the application needs to indicate to the client layer whether the data is interpreted as valid input or not. 

    In this case, in order to show whether the submitted data was correct (or not), the PHP checking routines will send back to the client a text-based response, which will present the following structure:

      field name|error message

    As you can see, this simple string that uses a (|) pipe delimiter for sending separated data will indicate to the client which form field is the offending one, and at the same time, what type of error message will be displayed on the web document. Of course, there are many ways to define the interaction between the client and the server, and I particularly picked up this one; however you can use the method that best suits your needs, without having to rewrite the whole application.

    All right, now that you know how the interaction between the client application and the PHP validation routines will work, take a look at the definition of the “displayError()” function:

      function displayErrorMessage(serverResponse){
        var elemkey=serverResponse.split('|')[0];
        var errormsg=serverResponse.split('|')[1];
        var counter=0;
        var msgcont=document.getElementById('msgcontainer');
        if(msgcont){msgcont.parentNode.removeChild(msgcont)};
        var msgcont=document.createElement('div');
        msgcont.setAttribute('id','msgcontainer');
        // assign error values to error counters
        errors[elemkey]=(errormsg==' ')?0:1;
        // count total errors
        for(var i in errors){if(errors[i]){counter++}};
        var btn=document.getElementsByTagName('form')[0].elements[3];
        if(!counter){
            // if no errors were found enable submit button
            btn.disabled=false;
            var errormsg='Data is Ok. Now submit the form, please.';
        }
        else{
            // if errors were found enable submit button & display
    error message
            btn.disabled=true;
        }
        msgcont.appendChild(document.createTextNode(errormsg));
        document.getElementById('formcontainer').appendChild
    (msgcont);
       
      }

    As shown above, the “displayErrorMessage()” function is quite simple to understand. In simple terms, all that it does is receive the response sent by the server, and split it up into two corresponding parts. The first one is then used for counting the errors that occurred with reference to each form field (the error array is used for doing this), while the second one is utilized for displaying the corresponding error message when wrong data was entered by the user.

    According to the programming logic previously described, if the “errors” array, which is used for storing the errors of each specific form field, doesn’t contain any values, then all the data entered in the online form has been considered valid by the PHP routines. What does this mean, after all? Well, it implies that the form can be submitted safely, since all the user inputs are correct.

    Finally, the previous function displays a confirmation message and enables the respective form submitting button. This little add-on should be used with a certain amount of caution, since its functionality depends completely on using script-enabled browsers. Nevertheless, I designed the application in such a way that the form can be submitted normally when JavaScript has been disabled by the client, thus this issue shouldn’t be a big concern to you.

    Having explained how the “displayErrorMessage()” function works, I’d like to show you another useful JavaScript function, which is also part of this AJAX-based application. In this case, I’m talking about the “initializeForm()” function, which I’ll explain in the upcoming section. 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