27.10.2015 Views

AJAX and PHP

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>AJAX</strong> Real-Time Charting with SVG<br />

coordinates, <strong>and</strong> based on that data, the server generates a new set. This simulates pretty well a<br />

real-world scenario. The previously generated coordinates are sent via GET as two parameters<br />

named lastX <strong>and</strong> lastY. To test the server-side functionality independently of the rest of the<br />

solution, try loading http://localhost/ajax/svg_chart/svg_chart.php?lastX=5&lastY=44:<br />

Figure 7.3: The Server generating a New Set of Coordinates for the Client<br />

On the client, everything starts with index.html, which is really simple; all it does is to load the<br />

SVG file. The best way to do this at the moment is by using an element, which isn't<br />

supported by W3C (you can also use <strong>and</strong> , but they are more problematic—see<br />

http://www.w3schools.com/svg/svg_inhtml.asp):<br />

<br />

<br />

<br />

And here it comes—chart.svg. This file isn't very impressive by itself, because it uses functionality<br />

provided by the JavaScript files (notice the onload event), but it includes the chart title:<br />

<br />

<br />

<br />

<br />

<br />

SVG with <strong>AJAX</strong> <strong>and</strong> <strong>PHP</strong> Demo<br />

<br />

<br />

<br />

chart.svg references two JavaScript files:<br />

ajaxRequest.js contains the engine that implements asynchronous HTTP request functionality<br />

using the XMLHttpRequest object. This engine is used by realTimeChart.js to get new chart data<br />

from the server when the Firefox web browser is used. For the Adobe SVG <strong>and</strong> Apache Batik<br />

implementations, we use specific functionality provided by these engines through their getURL<br />

methods instead. See the updateChart method in realTimeChart.js for details.<br />

The code in ajaxRequest.js contains a different coding pattern than what you've met so far in the<br />

book. What is important to underst<strong>and</strong> is:<br />

200<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!