This book uses PHP because it’s the most common choice for connecting to Ajax. PHP is easy to write and easy to use. There are thousands of Web servers that support PHP, so if you want to sign up for one, they’re easy to find. Your current server may already support PHP because most do these days _ just ask them. For testing purposes, you can also install PHP on your own machine. You can get PHP for free at www.php.net, complete with installation instructions (on Windows, installing can be as easy as running a .exe file).
For example, say that instead of downloading text from a static file, data.txt, you had this PHP script, data.php, installed on the server:
<?php echo 'This text was also fetched from the server with Ajax.'; ?>
This script sends (echoes) the text “This text was also fetched from the server with Ajax” back to the browser, so you should be able to display that text in your Ajax application if you fetch it from the server. Fetching that text from the server is easy; all you need to do is fetch data from the URL data.php, not data.txt, as you see here in index3.html:
You can see this Web page, index3.html, in Figure 3.3.
Figure 3.3: Reading text from a PHP script using Ajax
When the user clicks the button in this page, the text from the PHP script, data.php, is downloaded and displayed, as shown in Figure 3.4. Presto! You’re working with online server-side scripts and Ajax. Not bad.
Figure 3.4: Downloading text from a PHP script