Os Phpajax A4
Os Phpajax A4
How the Simple Ajax Toolkit can integrate your server-side PHP with JavaScript
Skill Level: Intermediate Tyler Anderson ([email protected]) Freelance Writer Stexar Corp.
18 Oct 2005 For years, the goal of creating a truly responsive Web application was hampered by one simple fact of Web development: To change the information on part of a page, a user must reload the entire page. Not anymore. Thanks to asynchronous JavaScript and XML (Ajax), we can now request new content from the server and change just part of a page. This tutorial explains how to use Ajax with PHP and introduces the Simple Ajax Toolkit (Sajax), a tool written in PHP that lets you integrate server-side PHP with JavaScript that makes this work.
Page 1 of 22
developerWorks
ibm.com/developerWorks
Prerequisites
The following tools are needed to follow along: Web server Pick any Web server and operating system. Feel free to use Apache 2.X or the IBM HTTPServer. PHP You can follow along without PHP, but if you are interested in interacting with the sample application download PHP V5. Sajax You will need Sajax. This is a single-file library of PHP functions used in this tutorial. Web browser You will need a Web browser that supports JavaScript. These include Mozilla, Firefox, Opera, and Microsoft Internet Explorer.
Section 2. Overview
Before diving in, let's meet Ajax, the sample PHP application, and Sajax.
Ajax
Ajax allows Web developers to create interactive Web pages without the bottleneck of having to wait for pages to load. Through Ajax, you can create applications that, with a click of a button, will replace content in one section of a Web page with totally new content. The beauty of it is that you don't have to wait for the page to load, except for the content to load for that single section. Take Google Maps, for example: You can click and move the map around without having to wait for page loads.
ibm.com/developerWorks
developerWorks
Sajax
Say you want to create an Ajax application without having to worry about the intricate details of Ajax. Sajax is the answer. Sajax abstracts away from you, the Web developer, the high-level details of Ajax through the use of a library developed by the folks at ModernMethod. Deep down, Sajax works the same as Ajax. However, the technical details of Ajax can be ignored through the use of higher-level functions provided in the Sajax library.
Using Ajax with PHP and Sajax Copyright IBM Corporation 1994, 2005. All rights reserved.
Page 3 of 22
developerWorks
ibm.com/developerWorks
These span tags are used in the sample application, and the color conforms to that used for links on all IBM developerWorks tutorials. The first line within the style tag specifies that when the link has been visited, the color will remain the same. Hovering over it will underline it, and the cursor will turn into a pointer, just like using regular anchor tags (<a href... >). Let's take a look at how to create links that refer to this CSS style code.
The onclick handler specifies which script to run when this span tag is clicked. There are several other specifiers similar to onclick you can experiment with, including onmouseover and ondblclick. Notice the JavaScript function, loadHTML, is shown instead of a traditional http:// link or a relative link created by listing panels-ajax.php? in the onclick field. You will learn about the loadHTML, function next.
ibm.com/developerWorks
developerWorks
If you are using Mozilla, Opera, or another browser in one of these genres, content will be dynamically fetched using built-in XMLHttpRequest objects. Microsoft's Internet Explorer browser uses a different object, which you will learn about next. They are used in essentially the same way, and providing support for both is only a matter of adding a few extra lines of code. The XMLHttpRequest object is used to retrieve page content in JavaScript. You will use this code later in the sample application, along with the appendages to the loadHTML function that covers the ActiveXObject. See Listing 2 for usage.
The destination variable, passed as a parameter in Listing 2, communicates where the content loaded by the XMLHttpRequest object will go, denoted by a <div id="content"></div> tag. Then the code checks whether the XMLHttpRequest object exists, and if it does, it creates a new one. Next, the event handler is set to the processStateChange function, which is the function the object will call on each state change. The rest of the request is setup using the open method that passes in the type of transfer, GET, and the URL that the object will load. The object is finally put to action by calling its send method.
The ActiveXObject
The ActiveXObject is used in place of the XMLHttpRequest object in Internet Explorer. Its function is identical to that of XMLHttpRequest, and even its function names are the same, as you can see in Listing 3.
Page 5 of 22
developerWorks
ibm.com/developerWorks
... function loadHTML(URL, destination){ dest = destination; if(window.XMLHttpRequest){ ... } else if (window.ActiveXObject) { request = new ActiveXObject("Microsoft.XMLHTTP"); if (request) { request.onreadystatechange = processStateChange; request.open("GET", URL, true); request.send(); } } } </script>
In this case (if you are using Internet Explorer), a new ActiveXObject is instantiated of the Microsoft.XMLHTTP type. Next, the event handler is set, and its open function is called. The object's send function is then called, putting ActiveXObject to work.
ibm.com/developerWorks
developerWorks
When the XML HTTP object reaches state 4, the content is ready to be extracted and displayed at the desired location on the browser. The location is contentDiv, and it is retrieved from the document. If the request was good and received in good order, the status of the response will equal 200. The HTML response is held at request.responseText, and it is displayed in the browser by setting it equal to contentDiv.innerHTML. If there were no errors in the transfer, then all went well and the new content will appear in the browser; otherwise, request.status will not equal 200. See Listing 5 for the error handling code.
Listing 5 will send information about the transfer error to the browser. You will use this function in your sample application as the callback function. Next, you will learn about the issues and differences between GET and POST.
Using Ajax with PHP and Sajax Copyright IBM Corporation 1994, 2005. All rights reserved.
Page 7 of 22
developerWorks
ibm.com/developerWorks
buy, or buy it again when you already have it. The implications of GET vs. POST have the same effects in Ajax. It's important to understand the differences between GET and POST requests while building this application, as well as your future applications. This will help you avoid one of the common pitfalls of Web application development.
Encoding methods
There are various ways to encode the transfer of data in HTTP, and XML only accepts a few of them. The one that maximizes interoperability is UTF-8 because it is backwards-compatible with American Standard Code for Information Interchange (ASCII). Also, there are numerous international characters used in other countries encoded in ways that are not backwards-compatible with ASCII, nor are they suitable for placing in XML files without proper encoding. For example, putting the string "Internationalization" in your browser transforms it to I%F1t%EBrn%E2ti%F4n%E0liz%E6ti%F8n, encoded using UTF-8. UTF-8 encoding of classical ASCII characters matches the 7-bit ASCII codes for the same characters, making UTF-8 an ideal choice for an encoding method selection. This is important to know because you deal with encoding all the time in the transfer and receipt of documents through HTTP, including Ajax. Transfers using Ajax should also use UTF-8 encoding because interoperability will improve through standardization.
This specifies the document type as HTML and the XML standard you will be using.
ibm.com/developerWorks
developerWorks
You may notice later on that if you remove this line when all is said and done, the display of the side panel will change slightly. Begin the HTML, specify the encoding and add the CSS style code as shown in Listing 6.
The title of the page will be the section name of a previously written tutorial. The encoding is specified using a metatag, and you have set up your HTML document to be able to use span tags as links. Next, you will create these links.
This header.html file contains CSS, and other JavaScript and formatting information used by developerWorks tutorials. It also sets up the page so you can start adding the links. There is an introduction to the section, and nine panels, so you will need to create 10 links, as shown in Listing 7.
Using Ajax with PHP and Sajax Copyright IBM Corporation 1994, 2005. All rights reserved.
Page 9 of 22
developerWorks
ibm.com/developerWorks
<span onclick="loadHTML('panels-ajax.php?panel_id=1', 'content')">Adding content</span> <?php require('content/between-link.html'); ?> <span onclick="loadHTML('panels-ajax.php?panel_id=2', 'content')">Saving new content</span> <?php require('content/between-link.html'); ?> <span onclick="loadHTML('panels-ajax.php?panel_id=3', 'content')">Editing content</span> <?php require('content/between-link.html'); ?> <span onclick="loadHTML('panels-ajax.php?panel_id=4', 'content')">Saving edited content</span> <?php require('content/between-link.html'); ?> <span onclick="loadHTML('panels-ajax.php?panel_id=5', 'content')">Avoid adding duplicates</span> <?php require('content/between-link.html'); ?> <span onclick="loadHTML('panels-ajax.php?panel_id=6', 'content')">Avoid editing a page that doesn't exist</span> <?php require('content/between-link.html'); ?> <span onclick="loadHTML('panels-ajax.php?panel_id=7', 'content')">Deleting content</span> <?php require('content/between-link.html'); ?> <span onclick="loadHTML('panels-ajax.php?panel_id=8', 'content')">Serving a page</span> <?php require('content/between-link.html'); ?> <span onclick="loadHTML('panels-ajax.php?panel_id=9', 'content')">Pulling it together: The CMS control page</span>
Every link calls loadHTML, passing the URL that has the panel ID, which will be used to determine what panel to load, with the second parameter specifying the destination div tag to place new content. Each link has some HTML code in between, and in order to abstract that away from the tutorial, the HTML was placed in between-link.html, which can also be downloaded on page . See Figure 1 for example browser output.
ibm.com/developerWorks
developerWorks
The links are shown on the left side, just like a single section of a tutorial on developerWorks.
This will add the section title, as well as print page links. Next, you will initialize the content section with all of the panels, one after another, within the content div tag, as shown in Listing 8.
Using Ajax with PHP and Sajax Copyright IBM Corporation 1994, 2005. All rights reserved.
Page 11 of 22
developerWorks
ibm.com/developerWorks
require('content/panel-2.html'); require('content/panel-3.html'); require('content/panel-4.html'); require('content/panel-5.html'); require('content/panel-6.html'); require('content/panel-7.html'); require('content/panel-8.html'); require('content/panel-9.html'); ?> </div>
All 10 panels will now be displayed one after another, in IBM's standard section format. To finish off the HTML, the next step is to require the footer file:
This finalizes the HTML for the page. See Figure 2 for sample browser output.
The content is initialized, and the links are ready to call JavaScript instructions.
ibm.com/developerWorks
developerWorks
the side.
This code loads the URL specified in the link: panels-ajax.php. Next, you will insert the processStateChange function.
Listing 10. Processing state changes and placing new content in the div
... var dest; function processStateChange(){ if (request.readyState == 4){ contentDiv = document.getElementById(dest);
Using Ajax with PHP and Sajax Copyright IBM Corporation 1994, 2005. All rights reserved.
Page 13 of 22
developerWorks
ibm.com/developerWorks
if (request.status == 200){ response = request.responseText; contentDiv.innerHTML = response; } else { contentDiv.innerHTML = "Error: Status "+request.status; } } } function loadHTML(URL, destination){ ...
You have completed the ajax-app.php file. Now you need to define the panels-ajax.php file pointed to by the panel links.
Returning content
When you click on one of the links in your application, the Ajax code will try to load panels-ajax.php. Create this file, and place it in the same directory as ajax-app.php. This file will process the panel_id variable submitted to it using GET.
Listing 11. Processes the panel_id variable and returns the correct panel
<?php switch($_GET['panel_id']){ case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: require('content/panel-'.$_GET['panel_id'].'.html'); break; default: print("No such category<br>"); } ?>
If the panel_id variable exists, the correct panel will be returned. The panel content HTML files are located in the downloadable zip file (ajax.sajax.source.zip from downloads on page ), along with their accompanying figures. See Figure 3 for example of the browser output when clicking on the "Avoid editing a page that doesn't exist" panel.
ibm.com/developerWorks
developerWorks
With the links live and Ajax at work, clicking on a link replaces the initialized content with the content specific to that panel. Next, you will add navigation links.
Listing 12. Processes the panel_id variable, and returns the correct pane
require('content/panel-'.$_GET['panel_id'].'.html'); $panel_id_next = $_GET['panel_id'] + 1; $panel_id_prev = $_GET['panel_id'] - 1; if($panel_id_prev >= 0){ print(" <span onclick=\"loadHTML('panels-ajax.php?panel_id=".$panel_id_prev."', 'content')\">Previous Panel</span> "); if($panel_id_next <= 9) print(" | "); } if($panel_id_next <= 9){ print(" <span onclick=\"loadHTML('panels-ajax.php?panel_id=".$panel_id_next."', 'content')\">Next Panel</span> "); } break; default:
Using Ajax with PHP and Sajax Copyright IBM Corporation 1994, 2005. All rights reserved.
Page 15 of 22
developerWorks
ibm.com/developerWorks
The next link will simply be a link to the currently requested panel ID plus one, and minus one for the previous link. The previous panel link will be displayed if it exists, meaning that it has a value greater than or equal to zero, and the next panel link will be displayed if it has a value less than or equal to nine. The actual links are created the same as the others you have already created, except they will change depending on the ID of the current panel. See Figure 4 for sample browser output containing the navigation links.
Clicking the links will navigate the section of the tutorial as you would expect, so when the reader reaches the bottom of a panel, clicking the next panel link will take them to the next panel. Again, without having to wait for the entire page to reload, the content of the current panel will be replaced with the next one. This completes the application. Next, you will learn about how to integrate your application with Sajax.
ibm.com/developerWorks
developerWorks
What is Sajax?
The Simple Ajax Toolkit (Sajax) is synchronous asynchronous JavaScript and XML. What makes it synchronous is that the details of the XML HTTP object used in your current Ajax application are abstracted away using the Sajax.php library file. This makes developing Ajax applications much easier because the chance of programming errors are reduced. Your links will also be much simpler because they will contain only function calls. Basically Sajax is a modular way of making Ajax applications through defined and dynamic function calls, making the application development process smoother.
Initializing Sajax
Now you will begin the Sajax application. Copy your ajax-app.php file and rename the copy to sajax-app.php. Keep this file in the same directory as your ajax-app.php file. Add the following code to the beginning of the file:
Using Ajax with PHP and Sajax Copyright IBM Corporation 1994, 2005. All rights reserved.
Page 17 of 22
developerWorks
ibm.com/developerWorks
First, you will set up the remote URL. Then you need to initialize Sajax and export "panels." This will initialize Sajax to later create JavaScript functions to handle requests associated with the "panels" content.
Listing 14. Links for the panel links in the Sajax application
<?php require('content/header.html'); ?> <span onclick="getPanel(0)">Managing content</span> <?php require('content/between-link.html'); ?> <span onclick="getPanel(1)" >Adding content</span> <?php require('content/between-link.html'); ?> <span onclick="getPanel(2)" >Saving new content</span> <?php require('content/between-link.html'); ?> <span onclick="getPanel(3)" >Editing content</span> <?php require('content/between-link.html'); ?> <span onclick="getPanel(4)" >Saving edited content</span> <?php require('content/between-link.html'); ?> <span onclick="getPanel(5)" >Avoid adding duplicates</span> <?php require('content/between-link.html'); ?> <span onclick="getPanel(6)" >Avoid editing a page that doesn't exist</span> <?php require('content/between-link.html'); ?> <span onclick="getPanel(7)" >Deleting content</span> <?php require('content/between-link.html'); ?>
ibm.com/developerWorks
developerWorks
<span onclick="getPanel(8)" >Serving a page</span> <?php require('content/between-link.html'); ?> <span onclick="getPanel(9)">Pulling it together: The CMS control page</span> <?php require('content/pre-content.html'); ?> ...
Clicking on the links will now call a different JavaScript function, which you will add next.
The first line makes a call to sajax_show_javascript, which imports the JavaScript functions needed by Sajax, and the other getPanel function you will add. These added JavaScript functions bring the core of the Sajax application to the browser, as PHP is no longer in use once the page is first loaded by the browser.
Page 19 of 22
developerWorks
ibm.com/developerWorks
<?php if($_GET['rs'] == 'panels'){ switch($_GET['rsargs'][0]){ case 0: ... case 9: print("##"); require('content/panel-'.$_GET['rsargs'][0].'.html'); $panel_id_next = $_GET['rsargs'][0] + 1; $panel_id_prev = $_GET['rsargs'][0] - 1; if($panel_id_prev > = 0){ print(" <span onclick=\"getPanel(".$panel_id_prev.")\">Previous Panel</span> "); ... print(" <span onclick=\"getPanel(".$panel_id_next.")\">Next Panel</span> "); ...
This file will check the variables submitted via GET. Notice that you sent "panels" to the sajax_import function. This should be the value of the rs variable in the GET array. If the value of $_GET['rs'] is panels, then the panel_id variable is contained in $_GET['rsargs'][0], which is the first parameter you sent to the x_panels function, auto-generated by the Sajax library. Moving on, and before returning the appropriate panel, your code must print out any two characters, as there appears to be a bug in the Sajax library. These characters will not show up in the HTML source of the displayed Web page. Next, you will have to replace the rest of the references to $_GET['panel_id'] with $_GET['rsargs'][0]. Last, you will need to modify the navigation links to look like the links you already modified in the sajax-app.php file. Swap the call to loadHTML with getPanel, passing the ID as before.
Figure 5. Sample browser output of the PHP application integrated with Sajax
The behavior and sample output of the application, shown in Figure 5, is the same as when you put it together with Ajax.
Section 6. Summary
Congratulations! You created an Ajax application in PHP and integrated it with Sajax successfully. Your application will save those that use it -- and your future asynchronous JavaScript applications -- a lot of bandwidth and time waiting for pages to load because the entire Web page will not have to load on each click, only
ibm.com/developerWorks
developerWorks
the necessary content. This enables you to create rich interactive applications that will become more common.
Using Ajax with PHP and Sajax Copyright IBM Corporation 1994, 2005. All rights reserved.
Page 21 of 22
developerWorks
ibm.com/developerWorks
Resources
Learn
See "Ajax and scripting Web services with E4X, Part 1" for an introduction to ECMAScript for XML (E4X), a simple extension to JavaScript that simplifies XML scripting. See "Ajax and scripting Web services with E4X, Part 2" to learn how to use E4X to build the server side and implement simple Web services in JavaScript. Learn the concepts behind Ajax and the fundamental steps to creating an Ajax interface for a Java-based Web application in "Ajax for Java developers: Build dynamic Java applications." Read Sam Ruby's Ajax considered harmful blog to learn more about the GET vs. POST issue and the importance of encoding in UTF-8. Read this basic how to use CSS tutorial. ASCII stands for American Standard Code for Information Interchange. The table of ASCII codes is helpful. See this handy UTF-8 encoding reference for ASCII-compatible multibyte Unicode encoding. Visit the developerWorks Open source zone for extensive how-to information, tools, and project updates to help you develop with open source technologies and use them with IBM's products.
Discuss
Visit the IBM developerWorks Ajax blog to read what your peers are saying about Ajax. Get involved in the developerWorks community by participating in developerWorks blogs.