PHP
  Home arrow PHP arrow Page 5 - PHP and JavaScript Interaction: Storing Data in the Client - Part 2
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
PHP

PHP and JavaScript Interaction: Storing Data in the Client - Part 2
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 15
    2005-05-09


    Table of Contents:
  • PHP and JavaScript Interaction: Storing Data in the Client - Part 2
  • A brief look at the "createJavaScript()" function
  • Working in the client side: the JavaScript news ticker
  • Defining the JavaScript functions
  • A practical approximation: showing the news ticker in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    PHP and JavaScript Interaction: Storing Data in the Client - Part 2 - A practical approximation: showing the news ticker in action
    ( Page 5 of 5 )

    Wouldn't be convenient to put all the pieces together and illustrate how the news ticker does its job? I totally agree. In a step-by-step sequence, here's our functional example: 

    First, let's create the "news.txt" file with some possible values:

    Google expands its search capabilities adding video searching. Are you ready to submit your personal video?

    PHP hits new levels of performance with PHP5. Visit www.php.net to find out more!

    Want to be completely protected from Internet attacks? Disconnect your computer right now!

    Interview with possible alien creature! He said they're using MySQL to store world domination plans.

    Monkey proved to be a lot smarter as a Linux system administrator in weird experiment. Oops, we're lost!

    Next, let's work with the code:

    <?php

    // include the PHP function

    require_once('createjavascript.php');

    // generate the "news" JavaScript array, populating it with news data file

    // here is where the PHP-JavaScript interaction takes place

    echo createJavaScript('news.txt','news');

    // display a regular HTML page including the JavaScript functions to generate the news ticker

    ?>

    <html>

    <head>

    <title>CLIENT-SIDE NEWS TICKER</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <script language="javascript">

    var numNews,counter;

    // count number of news

    numNews=news.length;

    // initialize news counter

    counter=0;

    createNewsDiv=function(){

    // create news containing <div> element

    var newsDiv=document.createElement('div');

    newsDiv.id='news';

    // insert news <div> into document structure

    document.body.appendChild(newsDiv);

    }

    rotateNews=function(){

    // get news containing <div>

    var newsDiv=document.getElementById('news');

    if(!newsDiv){return;}

    // create new <div> element

    var div=document.createElement('div');

    div.id='news';

    // create paragraph for each news line

    var p=document.createElement('p');

    // style <p> element

    p.style.fontFamily='Verdana';

    p.style.fontSize='11px';

    p.style.fontWeight='bold';

    p.style.color='#c00';

    if(counter==numNews){counter=0;}

    p.appendChild(document.createTextNode(news[counter]));

    // insert paragraph into <div> news

    div.appendChild(p);

    // replace old <div> node with new <div> node

    newsDiv.parentNode.replaceChild(div,newsDiv);

    counter++;

    // rotate news every 10 seconds

    setTimeout('rotateNews()',10*1000);

    }

    // execute functions when page is loaded

    window.onload=function(){

    if(document.getElementById){

    createNewsDiv();

    rotateNews();

    }

    }

    </script>

    <style type="text/css">

    /*define style for news container*/

    #news {

    background: #eee;

    padding: 2px 0px 2px 10px;

    border: 1px solid #000;

    }

    </style>

    </head>

    <body>

    </body>

    </html>

    That's all we need. Our news JavaScript ticker is running smoothly! Of course the above list is a rough presentation. We might move the JavaScript functions out to an external file, as well as our brief CSS rules. What's more, the complete Web page can be generated by using a template file, which rapidly separates content from logic and visual presentation. What else can we ask for?

    Bottom line

    In this second part, we've demonstrated how to use PHP-JavaScript interaction for building a nice extensible news ticker, hopefully showing that this kind of process is relatively easy to manipulate for specific purposes. But the best part is still coming! In the last part of this series, we'll move one step beyond using this approach by building a complete record paging system, all without getting our head into the server. To find out more, stay tuned!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT