JavaScript
  Home arrow JavaScript arrow Page 4 - Creating a Dynamic Banner System with AJAX
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

Creating a Dynamic Banner System with AJAX
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2007-07-30

    Table of Contents:
  • Creating a Dynamic Banner System with AJAX
  • Creating a system of dynamic banners
  • Displaying the banners on the browser using AJAX
  • Completing the dynamic banner system
  • Listing the banner application's full source code

  • 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


    Creating a Dynamic Banner System with AJAX - Completing the dynamic banner system


    (Page 4 of 5 )

    As you'll recall from the previous section, it's necessary to build a simple PHP script that is capable of sending back to the client the correct banner image, along with its respective URL, each time an HTTP request is triggered by the AJAX application that you saw previously.

    Therefore, below I included the definition of the aforementioned script, which simply fetches the data corresponding to each banner from a plain text file named "banners.txt."

    Here's how the script looks:

    (definition of "fetchbanner.php" file)

    <?php
    try{
      if(!$banners=file('banners.txt')){
        throw new Exception('Error reading banners file');
      }
      $bannerId=!$_GET['bannerid']||$_GET['bannerid']<0||$_GET
    ['bannerid']>count($banners)?0:trim($_GET['bannerid']);
      echo $banners[$bannerId];
    }                      
    catch(Exception $e){
      echo $e->getMessage();
      exit();
    }
    ?>

    Indeed, after studying the signature of the above script, you'll grasp very quickly how it works, since all it does is read the contents of the "banners.txt" file and send the appropriate banner data back to the client.

    In addition, I included below the definition of a basic "banners.txt" file, which has been previously populated with some sample banner images and a few fictional links:

    (definition of "banners.txt" file)

    banner1.gif|http://www.myhosting.com
    banner2.gif|http://www.myphp.com
    banner3.gif|http://www.myjs.com

    All right, at this point you hopefully understand how the above PHP script sends a different banner to the browser each time an HTTP request is triggered via AJAX. However, I'm sure you want to see the full source code of this banner application, so you can grasp more easily how the modules of this application are linked to each other. 

    In the following section I'm going to show you all of the source files that comprise this application. Jump ahead and read the next few lines. I'll be there, waiting for you.

    More JavaScript Articles
    More By Alejandro Gervasio


       · If you're a web developer who wants to find other uses for AJAX, this series...
     

    JAVASCRIPT ARTICLES

    - Using jQuery to Preload Images with CSS and ...
    - Using Client-Side Scripting to Preload Image...
    - Removing Non-Semantic Markup when Preloading...
    - Using the Display CSS Property to Preload Im...
    - Preloading Images with CSS and JavaScript
    - Scaling and Moving Web Page Elements with th...
    - Fading, Hiding and Sliding HTML Elements wit...
    - Toggling CSS Properties with the GX JavaScri...
    - Cancel, Queue and Pause Animations with the ...
    - Producing Elastic Effects with the GX JavaSc...
    - Moving Divs Diagonally with the GX JavaScrip...
    - Moving Elements Vertically and Horizontally ...
    - Making Bouncing Effects in Parallel with the...
    - Creating Bouncing Effects with the GX JavaSc...
    - Manipulating Background Colors with the GX J...







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