JavaScript
  Home arrow JavaScript arrow Page 3 - Using Click Interceptions with JavaScript
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? 
JAVASCRIPT

Using Click Interceptions with JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-11-05

    Table of Contents:
  • Using Click Interceptions with JavaScript
  • Building a web form for using click interceptions
  • Using click interceptions for submitting and validating a web form via Ajax
  • Full source code for the click interceptions demonstration

  • 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


    Using Click Interceptions with JavaScript - Using click interceptions for submitting and validating a web form via Ajax


    (Page 3 of 4 )

    Since my intention here is to demonstrate how click interceptions can be used to submit and eventually validate a simple contact form with Ajax, I'm going to define a JavaScript function that permits the sending of an HTTP request by using a regular XML HTTP Request object.

    Here is the complete signature of this brand new function, which can fetch files in the web server with Ajax:


    // send http requests

    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);

    }


    As you can see, the previous "sendHttpRequest()" JavaScript function implements all the logic required to send HTTP requests via Ajax. Additionally, it will send the corresponding web server responses to another callback function, in this case called "displayErrorMessage()," which not surprisingly will be tasked with displaying, in the browser, all of the errors that might occur when validating the sample web form that I coded in the prior section.

    The definition of the "displayErrorMessage()" callback function mentioned before is as follows:


    // display error messages

    function displayErrorMessage(errorMessage){

    var errorcont=document.getElementById('errorcontainer');

    if(!errorcont){return};

    errorcont.innerHTML=errorMessage;

    }


    Indeed, the logic implemented by the above JavaScript function is pretty easy to follow. All it does is fill in the previously defined "errorcontainer" DIV with the different error messages generated after validating the pertinent online contact form.

    So far, so good, right? At this point, you've surely grasped how the two previous JavaScript functions handle the submitting and validating of a simple contact form with Ajax. However, there's an important piece of this schema that still remains undone.

    Yes, you guessed right! In this case, it's necessary to implement the programming logic required to intercept all the mouse clicks that will occur when the contact form is submitted by an user, and handle this operation with Ajax, in this way stopping the form's default behavior.

    The function listed below, called "initializeForm()," is the one responsible for performing the aforementioned click interception process. Here it is:


    // initialize web form

    function initializeForm(){

    var formbtn=document.getElementsByTagName('form')[0].elements[3];

    if(!formbtn){return};

    // use click interception to submit the form

    formbtn.onclick=function(){

    var theform=document.getElementsByTagName('form')[0];

    sendHttpRequest('processform.php?fname='+theform.elements[0].value+'&lname='+theform.elements[1].value+'&email='+theform.elements[2].value,'displayErrorMessage');

    return false;

    }

    }


    As shown by the above function, each time the previous contact form is submitted by a user, it will be intercepted in the middle of this process, and the pertinent form data will be sent to the web server via Ajax. Finally, the "return false" statement at the end of the function prevents the form from being sent in the regular fashion.

    All right, at this point I'm sure you have a much clearer idea of how to use a simple JavaScript click interception to submit a sample web form with Ajax. Considering that you've already learned the basics of implementing this handy approach, in the next section I'll be listing the complete source code of the previous hands-on example, and include the small PHP file that actually validates the online contact form.

    Jump ahead and read the next few lines. I'll be there, waiting for you.

    More JavaScript Articles
    More By Alejandro Gervasio


       · Click interceptions are a handy technique used for building JavaScript applications...
       · What is the purpose/function of the initializeForm function?
       · Thanks for commenting on my article. As I explained in the article, the...
     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







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