Jump to content
Larry Ullman's Book Forums

Recommended Posts

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,

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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>

  • Upvote 1
Link to comment
Share on other sites

Thanks for sharing that code, but I'm confused by what you're wanting to do. The book has an Ajax version of stores, I thought you wanted to create the non-Ajax version. I'm not clear as to what you're looking for, or what the code you've just posted is supposed to do.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Hello Roger,

 

Sorry for the confusion. From looking at your code, it seems that you've got one script that has the form and retrieves the data from the database. Your one script also has all the necessary HTML, but also has some JavaScript as if it were an Ajax version. Using an Ajax model, one script would have the form (it could be an HTML page, without any PHP). The form calls a JavaScript function that performs an Ajax request of the server. That file on the server only returns the data, perhaps in JSON format. The server-side script does not have any HTML in it (as in stores_json.php).

 

And to clarify, this chapter DOES have the complete code for demonstrating Ajax. What it doesn't have is the non-Ajax version, to be used as a fall-back should the user's browser not support JavaScript (which is uncommon these days). And that non-Ajax version is something any intermediate level PHP programmer could easily create. So the find_stores.php script, which is the non-Ajax version not included in the book, would be a lot like your code, but without the unnecessary JavaScript, JSON, and so forth.

 

Again, in simple terms:

Ajax process: HTML form -> submission -> call JavaScript function -> JavaScript function calls server script -> server script returns data -> another JavaScript function handles the data and updates the HTML page

Non-Ajax process: HTML form -> submission -> server script creates entire HTML page, including displaying data based upon form submission

 

Let me know if that's still not clear or if I'm incorrect about your script.

Link to comment
Share on other sites

Hello Roger,

 

Sorry for the confusion. From looking at your code, it seems that you've got one script that has the form and retrieves the data from the database. Your one script also has all the necessary HTML, but also has some JavaScript as if it were an Ajax version. Using an Ajax model, one script would have the form (it could be an HTML page, without any PHP). The form calls a JavaScript function that performs an Ajax request of the server. That file on the server only returns the data, perhaps in JSON format. The server-side script does not have any HTML in it (as in stores_json.php).

 

And to clarify, this chapter DOES have the complete code for demonstrating Ajax. What it doesn't have is the non-Ajax version, to be used as a fall-back should the user's browser not support JavaScript (which is uncommon these days). And that non-Ajax version is something any intermediate level PHP programmer could easily create. So the find_stores.php script, which is the non-Ajax version not included in the book, would be a lot like your code, but without the unnecessary JavaScript, JSON, and so forth.

 

Again, in simple terms:

Ajax process: HTML form -> submission -> call JavaScript function -> JavaScript function calls server script -> server script returns data -> another JavaScript function handles the data and updates the HTML page

Non-Ajax process: HTML form -> submission -> server script creates entire HTML page, including displaying data based upon form submission

 

Let me know if that's still not clear or if I'm incorrect about your script.

 

Hi Larry,

 

Thanks for the clarification. This makes it clearer. It is much appreciated.

Link to comment
Share on other sites

 Share

×
×
  • Create New...