Jump to content
Larry Ullman's Book Forums

rwg

Members
  • Posts

    8
  • Joined

  • Last visited

rwg's Achievements

Newbie

Newbie (1/14)

1

Reputation

  1. Hi Larry, Thanks for the clarification. This makes it clearer. It is much appreciated.
  2. 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
  3. 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
  4. 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>
  5. 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
  6. 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
  7. Hi Jonathon, Thanks for the reply. The code for stores.php and find_stores.php are not included in the download. Thanks
  8. 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,
×
×
  • Create New...