PHP
  Home arrow PHP arrow Page 4 - PHP and JavaScript, Pooling Your Resources
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? 
Google.com  
PHP

PHP and JavaScript, Pooling Your Resources
By: Brian Vaughn
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 35
    2005-09-20


    Table of Contents:
  • PHP and JavaScript, Pooling Your Resources
  • PHP Disguised as JavaScript?
  • You Want More JavaScript?
  • How Does This Work?

  • 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, Pooling Your Resources - How Does This Work?
    ( Page 4 of 4 )

    For our first application, we’ll keep things simple. The primary focus here is to demonstrate how our two languages interact. To do that, we will construct two files: the first a simple PHP page that comprises mainly HTML, and the second a PHP page that generates JavaScript. Our first page will be as follows:

    <?php
          # start the session, and clear any existing values
          session_start();
          $_SESSION['js_count']   = 0;
    ?>
    <!-- main HTML display -->
    <HTML>
          <head>
                <title>PHP & Javascript - Example</title>
                <STYLE type='text/css'>
                      /* set Body display properties to make things pretty */
                      body, p, th, td {
                            font-family: Tahoma, Arial, Helvetica, sans-serif;
                            font-size: 11px;
                      }
                </STYLE>
                <SCRIPT type="text/javascript">    
                      function attach_file( p_script_url ) {
                            // create new script element, set its relative URL, and load it
                            script = document.createElement( 'script' );
                            script.src = p_script_url;
                            document.getElementsByTagName( 'head' )[0].appendChild( script );
                      }
                </SCRIPT>
          </head>    
          <body>

                <!-- call external PHP / Javascript file when clicked -->
                <a href="javascript:attach_file( 'javascript.php' )">What time is it?</a>
                <br><br>
                <span id="dynamic_span" />
          </body>
    </HTML>

    As you can see, things are pretty simple at this point. The PHP file initiates a SESSION variable, ‘js_count’ to be 0 and then outputs a simple HTML page. The only JavaScript present so far is the function we discussed earlier. An anchor tag is displayed to allow the user to call that function. A ‘dynamic_span’ tag is also present, and we’ll see why shortly. Now let’s take a look at our other PHP/JavaScript file, named ‘javascript.php’:


    <?php
                # set appropriate content type - to tell the browser we're returning Javascript
                header( 'Content-Type: text/javascript' );         

                # start the session
                session_start();           

                # determine the current time
                $time                = date( 'g:i a' );           

                # determine how many times the user has called the function, then increment it
                $js_count          = ( ++$_SESSION['js_count'] );           

                # based on the count, set the appropriate display value
                if ( $js_count == 1 ) {
                            $inner_html       = "This is your first time asking. It is $time.";
                } else if ( $js_count <= 3 ) {
                            $inner_html       = "You have asked me $js_count times now. It is $time.";
                } else {
                            $inner_html       = "You have already asked me $js_count times. Shut up!";
                } # END big-nasty-else-if-block
    ?>
    // retrieve span object, and attach additional HTML to it
    dynamic_span_obj          = document.getElementById( 'dynamic_span' );
    dynamic_span_obj.innerHTML     += '<?php echo $inner_html; ?> <br>';

    Once again this file is pretty simple. Our PHP code simply tells the browser that it is a JavaScript file, then it retrieves the ‘js_count’ SESSION variable and creates a return String value depending on how many times the file has been called. Underneath our main PHP, our JavaScript simply updates the ‘dyamic_span’ tag on the main page, appending the PHP-generated String value.

    That’s it?

    That’s it. Try it out! So far we’ve kept things very simple, but hopefully you are beginning to see the possibilities that this type of interaction between PHP and JavaScript allows for. Part 2 of this article will then continue by expanding our example application to include some additional, user-friendly features as well as to implement a more practical scenario with PHP pulling its information from a MySQL database.



     
     
    >>> More PHP Articles          >>> More By Brian Vaughn
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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...





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