elements needed to flesh out the select object.
Writing the loadList.php program
Of course, you need to have a PHP program on the server to do the work. AJAX makes PHP programming a lot simpler than the older techniques, because each PHP program typically solves only one small problem, rather than having to build entire pages. The loadList.php program is a great example:
//connect to database
$db = mysql_connect(“localhost”, “username”, “password”) or die(mysql_error);
mysql_select_db(“xfd”);
//build an option element for each hero
$query = “SELECT * FROM hero”;
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
print <<< HERE
$row[name]
HERE;
} // end while
?>
The code for loadList.php is typical of PHP programs using AJAX. It’s small and focused and does a simple job cleanly. (I tend to think of PHP programs in AJAX more like external functions than complete programs.) The key to this particular program is understanding the output I’m trying to create. Recall that this example has an empty select element on the form. I want the program to add the following (bold) source code to the page:
The Plumber
Binary Boy
The Janitor
It should go to the database and find all the records in the hero table. It should then assign heroID to the value attribute of each option, and should display each hero’s name. After you know what you want to create, it isn’t difficult to pull off:
1. Make a database connection.
In this example, PHP is used mainly for connecting to the database. It’s not surprising that the first task is to make a data connection. Build a connection to your database using the techniques outlined in Book V, Chapter 7.
2. Create a query to get data from the database.
The option elements I want to build need the heroID and name fields from the hero database. It’s easiest to just use a SELECT * FROM hero; query to get all the data I need.
3. Apply the query to the database.
Pass the query to the database and store the results in the $result variable.
4. Cycle through each record.
Use the mysql_fetch_assoc() technique described in Book V, Chapter 7.
5. Build an element based on the current record.Because each record is stored as an associative array, it’s easy to build an option element using fields from the current record.
6. Print the results.
Whatever you print from the PHP program becomes the contents of the jQuery element that called the load() method. In this case, the elements are placed in the object (where all good option elements live).
Responding to selections
After the page has initialized, the select object contains a list of the heroes. When the user selects a hero, the showHero() function is called by the select element’s onchange event.
The showHero() function is another AJAX function. It gathers the details needed to trigger another PHP program. This time, the PHP program needs a parameter. The showHero() function simulates a form with a data element in it, and then passes that data to the PHP through the AJAX load() method:
function showHero(){
//pass a hero id, retrieve all data about that hero
heroID