PHP
  Home arrow PHP arrow Page 5 - PHP and JavaScript Interaction: Storing Data in the Client, part 1
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 1
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 46
    2005-05-04


    Table of Contents:
  • PHP and JavaScript Interaction: Storing Data in the Client, part 1
  • Lord of the Arrays
  • Building JavaScript Arrays with PHP: the "createJavascript()" function
  • Dealing with result sets
  • Getting practical: putting the "createJavaScript()" function into 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 1 - Getting practical: putting the "createJavaScript()" function into action
    ( Page 5 of 5 )

    In order to see the practical uses of our newly developed function, let's create an example to illustrate its functionality. Say we need to read information from a simple text file that contains some information about music albums, and store that data in a JavaScript array, which will be processed at later time. Our "album.dat" file might look as simple as this:

    Tales of Mystery and Imagination
    The Best of The Alan Parsons Project
    I Robot
    Eye In The Sky
    The Turn of a Friendly Card
    Stereotomy
    The Definitive Collection
    Vulture Culture
    The Instrumental Works
    Pop Classics

    Now, we can call our function in the following way:

    echo createJavaScript('album.dat','album');

    That's really simple, right? In this example, we've invoked the function to read data from our "album.dat" file, specifying that the information has to be stored in a JavaScript array named "album." So, what's the point of that? Well, if we take a look at the document source code, we find that the function has nicely created the array structure, storing each file line as array elements, as listed below:

    <script>
      var album=[];
      album[0]="Tales of Mystery and Imagination";
      album[1]="The Best of The Alan Parsons Project";
      album[2]="I Robot";
      album[3]="Eye In The Sky";
      album[4]="The Turn of a Friendly Card";
      album[5]="Stereotomy";
      album[6]="The Definitive Collection";
      album[7]="Vulture Culture";
      album[8]="The Instrumental Works";
      album[9]="Pop Classics";
    </script>

    I think that you understand now the capabilities of the function. With a few lines of code, the data is now available to us in a JavaScript array. Undoubtedly, we're really storing data in the client. Let's think about its possible uses and build an application to manipulate this data, without loading the server with unnecessary requests. 

    Summary

    In this first part of this series, we've implemented a simple mechanism to make PHP and JavaScript interact, creating a function which, once fed with a data source (i.e. files of database records), builds an array structure and stores the information in it. This allows for programmatic data manipulation without server interaction.

    However, we must be fair about this method and show its pitfalls too. The major issue is, definitely, its strong dependence on JavaScript. The second problem is the limited amount of stored data allowable in the client's machine, without compromising the system's stability.

    However, despite its weaknesses, the method is still viable for small applications, possibly in intranet environments, where we work with controllable client factors such as scripting enabled or specific hardware requirements. But there are a couple of uses to be reviewed yet. In the second part of this tutorial, we'll use our recently developed PHP function to build a file-based JavaScript ticker, in order to show a real application. In the meantime, take your time and have some fun playing with the code. See you in the next part!



     
     
    >>> 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 4 Hosted by Hostway
    Stay green...Green IT