5.10. Just Do It SolutionBelow 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. } } }
...and the user gets a really useless error message from the web form. Who's going to know what error code "400" means?
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... |