PHP
  Home arrow PHP arrow Page 5 - PHP and JavaScript Interaction: Storin...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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: 4 stars4 stars4 stars4 stars4 stars / 37
    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:
      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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · The article shows a simple and effective way to create and populate a JavaScript...
       · The article is very understanding. I hope the 2nd part is coming soon, because I do...
       · Hi friend,This first part implements a JavaScript array built with server-side...
       · Hi...This article is really very nice. It's also written in simple to understand...
       · Hi Sukhi,Thanks a lot for the compliments about my PHP-JavaScript related...
     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway