Section 5.10. Just Do It Solution


5.10. Just Do It Solution

Below is the completed showConfirmation() callback function; make sure your answers match up with ours... and make sure you understand all this code.

 function showConfirmation() {Remember, this is run after the server places the customer's pizza order. The response from the server will be the estimated time until the pizza is delivered.  if (request._readyState_ == __4__) {Be sure you check the ready state before checking the request's status.   if (request.__status__ == _200_) {    var response = request._responseText_;The request's responseText gives us the delivery time estimate.    // Locate form on page    var mainDiv = document.getElementById  ("_main-page__");    var orderForm = document.getElementById("_order-form_");Here, we get a reference to the <form> element node.    // Create some confirmation text    pElement = document._createElement__("p");    textNode = document._createTextNode_(Next, use the DOM to create some new text for a <p> element.      "Your order should arrive within " +      __response__ +      " minutes. Enjoy your pizza!");      pElement._appendChild__(textNode);Don't forget to add the text node to the <p> element.     // Replace the form with the confirmation     mainDiv.replaceChild(_pElement_, _orderForm_);The first argument is the new <p> element, and the second argument is the <form> to replace.   } else {     alert("Error! Request status is " + request._status__);If the PHP script sends back an error code, this will print that code out for the customer to see.   }  } } 

When things go wrong Solutions

So what do you think went wrong in each of the cases below? See how your answers compared with ours:

Walt mistypes his phone number into the Break Neck order form, and then enters his address and order manually.

As long as Walt typed his address correctly, there won't be a problem. He'll get his pizza.

This really isn't an error at all.

Maylee enters her phone number and pizza order, but accidentally changes the street number in the address field.

Alex will end up delivering the pizza to the wrong address! Nothing we can do about this...

There's not much we can do to prevent this.

Susan enters her phone number, but accidentally clicks "Order Pizza" before she enters her pizza order.

The pizza form will show an alert box with the error code returned from placeOrder.php, which ias "400".

In this case, the status code is not "200", so the "else" block of code in showConformation() gets run...


...and the user gets a really useless error message from the web form. Who's going to know what error code "400" means?

BRAIN POWER

Take a look at the last error, when placeOrder.php returns an error status code to the browser. Is there anything you could do to make the error message that the customer sees any more helpful?


Wait a second... doesn't placeOrder.php return an error message along with any error status codes? If we show that error message to the customer, they'll know what went wrong.

Error messages are a good thing

Flip back to the placeOrder.php script on page 284, and look at those lines at the top that check and make sure the customer's address and pizza order were received. If there's a problem, the script returns a status code of "400" along with an error message, and stops processing the customer's order.

But, our JavaScript callback, showConfirmation(), doesn't check for an error message from the script; it just shows the request object's HTTP status code if it's not 200. That's not very helpful...




Head Rush Ajax
Head Rush Ajax (Head First)
ISBN: 0596102259
EAN: 2147483647
Year: 2004
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net