spacer
home / programming / javascript / jf / column14 / 1 current pageTo page 2
[next]

Senior Applications Developer
Professional Technical Resources
US-OR-Portland

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Some Ankle With Visual Studio
Gentoo Linux Cancels Distribution
It's Official: Windows 7 at PDC, WinHEC

Developing Web Applications with Ajax, Pt. 3

In part 3 of this series on Ajax, we'll learn how to use Ajax in conjunction with server-side processing and how these technologies can produce powerful web applications. If you're interested in learning how to create webapps like GMail or Google Maps, this is a basic approach (although those are much more complex than we will get into in this article). For this article, I use PHP as the server-side language, but Ajax is compatible with any server-side language, so feel free to use whichever language is your favorite!

Once more, we begin with code from the previous article, so you might need to read it for reference.

So here's the HTML page (with the JavaScript):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en" dir="ltr">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>Developing Web Applications with Ajax - Example</title>
  <script type="text/javascript"><!--
  function ajaxRead(file){
   var xmlObj = null;
   if(window.XMLHttpRequest){
      xmlObj = new XMLHttpRequest();
   } else if(window.ActiveXObject){
      xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
   } else {
      return;
   }
   xmlObj.onreadystatechange = function(){
    if(xmlObj.readyState == 4){
       processXML(xmlObj.responseXML);
    }
   }
   xmlObj.open ('GET', file, true);
   xmlObj.send ('');
  }
  function processXML(obj){
   var dataArray = obj.getElementsByTagName('users')[0].childNodes;
   var dataArrayLen = dataArray.length;
   var insertData = '<table><tr><th>'
    + 'Pets</th><th>Tasks</th></tr>';
   for (var i=0; i<dataArrayLen; i++){
     if(dataArray[i].tagName){
        insertData += '<tr><td>' + dataArray[i].tagName + '</td>'
                   +  '<td>' + dataArray[i].getAttribute('tasks') + '</td></tr>';
     }
   }
   insertData += '</table>';
   document.getElementById ('dataArea').innerHTML = insertData;
  }
  //--></script>
  <style type="text/css"><!--
  table, tr, th, td {
   border: solid 1px #000;
   border-collapse: collapse;
   padding: 5px;
  }
  --></style>
 </head>
 <body>
  <h1>Developing Web Applications with Ajax</h1>
  <p>This page demonstrates the use of Asynchronous Javascript and XML (Ajax) technology to
  update a web page's content by reading from a remote file dynamically -- no page reloading
  is required. Note that this operation does not work for users without JavaScript enabled.</p>
  <p>This page particularly demonstrates the retrieval and processing of grouped sets of XML data.
  The data retrieved will be output into a tabular format below.
  <a href="#" onclick="ajaxRead('data.php'); return false">See the demonstration</a>.</p>
  <div id="dataArea"></div>
 </body>
</html>
 

Note: The only modification was that we changed "data.xml" in our ajaxRead() call to "ajax.php." This is because we'll be using PHP to output XML (if you open the PHP file in your browser, it will appear as an XML file -- we're just going to do processing in that file that couldn't be done with simply XML). This is what the output of the PHP file will look like:

<?xml version="1.0" encoding="UTF-8"?>
<data>
 <pets>
   <Cat tasks="Feed, Water, Comb flees" />
   <Dog tasks="Feed, Water, Put outside" />
   <Fish tasks="Feed, Check oxygen, water pureness, etc." />
 </pets>
</data>

While this will be the output, we're going to retrieve this information from a MySQL database. Instead of having to modify the XML file from now on, we can modify the data in our database, extract it via PHP and retrieve it onto a page with Ajax -- all this, and still no page reloading occurs.

home / programming / javascript / jf / column14 / 1 current pageTo page 2
[next]



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data · Controllers: Programming Application Logic
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Sprint Launches Mobile WiMAX Network · Albatron Downsizes with the KI780G Mini-ITX Motherboard · Can't Find a Wi-Fi Network? Make Your Own.

Created: November 15, 2005

URL: http://webreference.com/programming/javascript/jf/column14/1