PHP
  Home arrow PHP arrow Page 2 - PHP and JavaScript, Pooling Your Resou...
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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, Pooling Your Resources (continued)
By: Brian Vaughn
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 19
    2005-09-27

    Table of Contents:
  • PHP and JavaScript, Pooling Your Resources (continued)
  • Separating the Logic
  • What Does PHP Do?
  • Main Layout
  • Is it PHP? Or Javascript?

  • 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


    PHP and JavaScript, Pooling Your Resources (continued) - Separating the Logic


    (Page 2 of 5 )

    Although more complex in nature than our previous application, this example will still be relatively simple. However it is good practice to separate the various elements of our project so that the logic and the display do not get unnecessarily mixed together. To help keep things separate, we will create the following files:

    index.php        This is our main display file, containing mostly HTML.

    functions.php    A helper file, used to retrieve SQL information.

    external.php     PHP file used to generate external Javascript code.

    javascript.js    Javascript helper file, contains misc. JS functions.

    stylesheet.css   Stylesheet for our document, controls visual layout.

    Again, many of these files could be combined, but by separating them we are making our code easier to maintain and expand later. Let’s take a closer look now at each of the files.

    What About the Javascript?

    Our Javascript file, ‘javascript.js’, contains five basic functions. The first will look familiar, but we made a couple of subtle changes that may warrant explanation:

    // call to external Javascript/PHP file and pass Parent ID
    function update_child( p_parent_option ) {     

          // create new script element and set its relative URL
    (including ID argument)
          script = document.createElement( 'script' );
          script.src = 'external.php?parent_id=' +
    p_parent_option.value;     

          // attach (load) script element to document head
          document.getElementsByTagName( 'head' )[0].appendChild( script );
    }

    As you can see from its commenting, the ‘update_child’ function receives an OPTION tag as a parameter (more specifically, the OPTION tag that has just been selected by the user). It then creates a new script element as before, and attaches it to the document’s <head>. One difference in this updated version is that it also passes a URL argument along to the external JavaScript file: the currently selected OPTION’s value, (which is in this case the primary key for our ‘parent_menu’ SQL record).

    The other four functions are also pretty simple, but can be very useful in terms of user interface design. As you will see shortly, our basic HTML file contains a <pre> element with the ID ‘hint_box’. This box is set to display “Ready…” by default, but will be updated when our ‘parent’ menu’s onChange event is fired. This is done in order to display helpful information to the user about our ‘parent’ SELECT menu and the information it contains. The functions we’ve written to do this are as follows:

    // update hint box value
    function display_hint( p_hint_text ) {
                document.getElementById( 'hint_box' ).innerHTML =
     '<strong>Status</strong>: ' + p_hint_text;
    }

    // clear interval (if one has been set) & update hint box value
    using helper function
    function reset_hint() {
                window.clearInterval( document.getElementById
    ( 'hint_box' ).interval );
                display_hint( 'Ready...' );
    }

    // clear interval (if one has been set) & update hint box value
    using helper function
    function show_hint( p_hint_text ) {
                window.clearInterval( document.getElementById
    ( 'hint_box' ).interval );
                display_hint( p_hint_text );
    }

    // set new interval
    function set_timeout() {
                document.getElementById( 'hint_box' ).interval =
    window.setInterval( 'reset_hint()', 2000 );
    }

    Each function is relatively simple, and probably self-explanatory with the help of the inline commenting. However, let’s take a brief look at each to make sure there is no confusion.

    The ‘display_hint’ function receives as a parameter a simple string of text. It then appends to that string the text “Status: ”, and updates our ‘hint box’ to display the resulting string. This function acts as a helper for ‘reset_hint’ and ‘show_hint’, which we will now look at.

    The ‘reset_hint’ function does two things. First it clears any interval that may be set for our ‘hint box’, then it updates the status text to show “Ready” using our helper function, ‘display_hint’.

    The ‘show_hint’ function acts in much the same way as the ‘reset_hint’ function does. First it clears any pre-existing interval, then it updates the ‘hint box’ display value using our helper function, ‘display_hint’. The only exception is that ‘show_hint’ displays the text it receives as a parameter, instead of displaying a default value.

    Lastly, the ‘set_timeout’ function simply creates an interval, and attaches it to our ‘hint box’. This interval then waits for approximately two seconds to elapse before calling the ‘reset_hint’ function to clear any rollover text and return to the default “Ready” message.

    More PHP Articles
    More By Brian Vaughn


       · The app works great, except that the hint box doesn't update dynamically in IE6, but...
       · Glad to hear it :)
       · I have a PHP page, which references a 'javascript/PHP' file. (The file uses PHP to...
       · This scripts work with Opera8.5Very nice. It will help me a lot.
       · you should be able to include the file either way, but I normally use the "<script...
       · thank you :) i'm glad to hear it!
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT