JavaScript
  Home arrow JavaScript arrow Page 3 - Making JavaScript Applications Degrade Gra...
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

Making JavaScript Applications Degrade Gracefully with AJAX and MySQL
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-07-02

    Table of Contents:
  • Making JavaScript Applications Degrade Gracefully with AJAX and MySQL
  • Fetching database rows using a typical approach
  • Displaying additional database records with AJAX
  • Putting all the pieces together

  • 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


    Making JavaScript Applications Degrade Gracefully with AJAX and MySQL - Displaying additional database records with AJAX


    (Page 3 of 4 )

    As I stated in the previous section, the next step consists of creating an AJAX-based script which will allow us to display the full contents of a specific article on the same web page that shows the articles' titles and authors (remember that this information is fetched earlier from a MySQL database table with PHP 5).

    That being explained, here's the signature of the aforementioned AJAX-based script. Have a look at it, please:

    // 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);
    }
    function showDetails(results){
      
    if(results){
        
    var divid=results.split('|')[0];
         
    var result=results.split('|')[1];
        
    var artdiv=document.getElementById('article'+divid);
        
    if(divid){
          
    var div=document.createElement('div');
          
    div.appendChild(document.createTextNode(result));
          
    artdiv.appendChild(div);
        
    }
      
    }
    }
    var artcont=document.getElementById('articlecontainer');
    if(artcont){
      
    var links=artcont.getElementsByTagName('a');
      
    if(links){
        
    for(var i=0;i<links.length;i++){
          
    links[i].onclick=function(){
            
    sendHttpRequest('showdetail.php?id='+this.href.split
    ('?')[1].substring(3),'showDetails');
            
    this.href='#';
           
    }
         
    }
       
    }
    }

    While the source code that corresponds to the above JavaScript snippet seems to be rather complex, actually it's very easy to grasp. In general terms, the snippet in question first uses the "sendHttpRequest()" function for fetching the "showdetails.php" file on the server via AJAX, and then uses another one, called "showDetails()," which is tasked with displaying on the browser the full details of a specific article.

    In this case, you may have noticed that each article's content is wrapped by a containing DIV identified as "articlecontainer." This characteristic is used by the aforementioned "showDetails()" function to insert the details of a concrete article into the appropriate DIV element.

    The previous JavaScript snippet finishes its execution by attaching the "sendHttpRequest()" function to all the links that were initially created to display the full contents of a specific article, and turns these links off by assigning a "#" character to their "href" property.

    A final note before I finish explaining how this AJAX-based script works: please notice that the "showDetails()" function splits the corresponding server response into two different strings using a ("|") character. We do this because the server will send an ID value along with the corresponding article's contents, which will be used by this function to insert these contents into the correct DIV. Sounds pretty simple, right?

    So far, so good. At this stage I built a JavaScript application that shows the corresponding details of a specific article on the same web page via an AJAX-based HTTP request. However, the functionality of the original PHP application, which is responsible for fetching the respective database records from MySQL remains nearly the same.

    This condition speaks for itself about the versatility of the approach that I used here to develop JavaScript functions that degrade gracefully when scripting is disabled on the browser.

    And speaking of fetching article data from MySQL with AJAX, in the last section of this tutorial I'm going to list the full source code corresponding to this database-driven application, so you can see more clearly how its different modules are linked with each other.

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

    More JavaScript Articles
    More By Alejandro Gervasio


       · Over the course of this last installment of the series, a simple MySQL-based...
     

    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 5 hosted by Hostway
    Stay green...Green IT