For anyone interested, I found a way to do the Ajax Version of Stores (Chapter 13).
find_stores.php (a combination of stores.html and stores_json.php) - see p521
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Find Stores</title>
<script src="ajax.js"
type="text/javascript"
language="javascript">
</script>
<script src="stores.js"
type="text/javascript"
language="javascript">
</script>
</head>
<body>
<?php
$zip = FALSE; // Flag variable.
// Validate that the page received $_POST['zip']:
if ( isset($_POST['zip']) &&
( (strlen($_POST['zip']) == 5) || (strlen($_POST['zip']) == 10) )
) {
// Chop off the last four digits, if necessary.
if (strlen($_POST['zip']) == 10) {
$zip = substr($_POST['zip'], 0, 5);
} else {
$zip = $_POST['zip'];
}
// Make sure it's numeric:
if (is_numeric($zip)) {
// Connect to the database:
$dbc = mysqli_connect ('localhost', 'admin', 'password', 'zips') OR die ('null');
// Get the origination latitude and longitude:
$q = "SELECT latitude, longitude FROM zipcodes1 WHERE zipcode='$zip'";
$r = mysqli_query($dbc, $q);
// Retrieve the results:
if (mysqli_num_rows($r) == 1) {
list($lat, $long) = mysqli_fetch_array($r, MYSQLI_NUM);
} else { // Invalid zip.
$zip = FALSE;
mysqli_close($dbc);
}
} else { // Invalid zip.
$zip = FALSE;
}
}
if ($zip) { // Get the stores and distances.
// Big, important query:
$q = "SELECT s.name,
CONCAT_WS('<br />', s.address1, s.address2),
z.city,
z.state,
s.zip_code,
s.phone,
ROUND(DEGREES(ACOS(SIN(RADIANS($lat))
* SIN(RADIANS(latitude))
+ COS(RADIANS($lat))
* COS(RADIANS(latitude))
* COS(RADIANS($long - longitude)))) * 69.09)
AS distance
FROM stores as s
LEFT JOIN zipcodes1 as z
on s.zip_code = z.zipcode
ORDER BY distance ASC
LIMIT 3";
if ($r = mysqli_query($dbc, $q)) {
$row_cnt = mysqli_num_rows($r);
}
else {
$row_cnt = 0;
}
if ( $row_cnt > 0 ) {
// Initialize an array:
$json = array();
// Put each store into the array:
while (list($name[], $address[], $city[],
$state[],
$zipcode[],
$phone[],
$distance[]
) = mysqli_fetch_array($r, MYSQLI_NUM)) {
$json[] = array(
'name' => $name,
'address' => $address,
'city' => $city,
'state' => $state,
'zip' => $zipcode,
'phone' => $phone,
'distance' => $distance);
}
// Send the JSON data:
echo "<font size=3 color=red>
json data:</font><br />";
echo json_encode($json) . "\n";
// Display the stores:
echo "<br /><br /><font size=3 color=red>
Here are your stores:</font><br />";
for ($row = 0; $row < $row_cnt; $row++) {
echo "<h3>$name[$row]</h3>
<p>$address[$row]<br />" .
ucfirst(strtolower($city[$row])) . ",
$state[$row]
$zipcode[$row]<br />
$phone[$row] <br />
(approximately $distance[$row] miles)
</p>
<br />\n";
} // End of WHILE loop.
} else { // No stores returned.
echo '<p class="error">No stores matched the search.</p>';
}
mysqli_close($dbc);
// Invalid zip
} else {
echo 'Enter a valid zipcode.';
}
?>
<form action="find_stores.php" method="post">
<p>
Your Zip Code:
<input name="zip"
type="text"
size="10"
maxlength="10" />
<input name="submit"
type="submit"
value="Find a Store"
onclick="return get_stores(this.form.zip.value)" />
</p>
</form>
<div id="list"></div>
</body>
</html>