"Beginning PHP and MySQL E-Commerce: From Novice to Professional, Second Edition"
Load your catalog, and ensure that the Add to Cart buttons work fine using the new AJAX code. CHAPTER 13 ?– IMPLEMENTING AJAX FEATURES 421 How It Works We??™ve modified the name of the submit HTML element to add_to_cart so that we can submit it from JavaScript: {/section}
{* Add the submit button and close the form *}
We have a variable named showErrors, whose value affects the way errors are handled. If this variable is true, debugging error messages are displayed using alert(). This isn??™t a very user-friendly method of handling errors, but it helps with debugging. If showErrors is false, no errors are shown. Instead, when a JavaScript error happens, the behavior is degraded to a classical form submit. // Display error messages (true) or degrade to non-AJAX behavior (false) var showErrors = true; This variable is used in the handleError() function, which is called every time an error happens in the JavaScript code. If showErrors is true, this function simply displays the error details: // Displays an error message or degrades to non-AJAX behavior function handleError($message) { // Ignore errors if showErrors is false if (showErrors) { // Display error message alert("Error encountered: \n" + $message); return false; } If showErrors is false, we don??™t display error details.