rwg
-
Posts
8 -
Joined
-
Last visited
Posts posted by rwg
-
-
Okay, then. To me, the code you posted does not demonstrate Ajax at all, but perhaps I'm missing something. The important thing is that you're satisfied with where you're at.
Hi Larry,
If my code doesn't demonstrate ajax, then I clearly don't understand something. I think it would be helpful if in the next version of the book, you provide the complete code to demonstrate the principle.
Thanks
-
Hi Larry,
As I started this thread "The Ajax Find Stores Application does not work out of the book". The stores.html didn't work for me. My quest was to find a way to get ajax work. I am satisfied now. I shared the code in case other's were also confused. I don't make any claim that the code is good, only that it demonstrates ajax.
Thanks
-
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>
- 1
-
I tried using distance.php, which only works if I disable stores.js. This is NOT the solution.
I reread the note from Paul Swanson. Paul, I think you are correct. find_stores.php should be modeled after stores_json.php.
I hope to get this right yet.
Thanks
-
Sorry, that should refer to stores.html, not stores.php. What I'm saying is that the book demonstrates how to find and display the stores listing using Ajax (in using stores.html and the JavaScript). For fallback reliability, you could create find_stores.php, which handles the form submission should the user's browser not support JavaScript. I didn't create find_stores.php in the book because it's a very simple script that any PHP programmer should be able to write (and it's standard PHP, not Ajax). If you're having any problems writing your own find_stores.php, let us know.
Sorry for the confusion.
OK. Let me see if I have this.
stores.html goes to find_stores.php to process the form, which has the user's zipcode. So, find_stores.php should be like distance.php from Chapter 3, except that it accepts the form data and produces the list of stores.
If this is the intent then the book on p521 should reference distance.php instead of stores.php. Is this correct?
I realize there are lots of ways of skinning a cat. I just want to understand the text.
Thanks
-
You can download the source for this book from www.larryullman.com/books/
Hi Jonathon,
Thanks for the reply. The code for stores.php and find_stores.php are not included in the download.
Thanks
-
The Ajax Find Stores Application does not work out of the book. The book, p521, says "... the form would be submitted to find_stores.php. I have not, in this chapter, created find_stores.php, but it would do exactly what stores.php does, except it would ..." I cannot find any reference to stores.php anywhere in the book. Do you have a sample of the find_stores.php code?
Thanks,
Chapter 13
in PHP 5 Advanced: Visual QuickPro Guide (2nd Edition)
Posted
Hi Larry,
Thanks for the clarification. This makes it clearer. It is much appreciated.