294
Chapter 5
Send OrderHTML Form PHP Script Callback
Now that you know how to get a response header from the server, you can
improve the Break Neck JavaScript, and let customers know a little more
about any errors they run into.
Let’s make a few simple changes to pizza.js:
Break Neck error handling
function showConrmation() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseText;
var mainDiv = document.getElementById(“main-page”);
var orderForm = document.getElementById(“order-form”);
pElement = document.createElement(“p”);
textNode = document.createTextNode(
“Your order should arrive within “ +
response +
“ minutes. Enjoy your pizza!”);
pElement.appendChild(textNode);
mainDiv.replaceChild(pElement, orderForm);
} else {
var message = request.getResponseHeader(“Status”);
if ((message.length == null) || (message.length <= 0)) {
alert(“Error! Request status is “ + request.status);
} else {
alert(message);
}
}
}
}
OK, so
now
we’re ready to go, right?
If placeOrder.php reports an
error, the status code will
be “400”, so the “else” part
of this code will run.
This gets the value of the “Status”
response header, if there is one.
If the server or script returned a
“Status” response header, then show
that to the customer.
If there’s no “Status” response
header, then just print out the
error code, like we did in the older
versions ...