Open ajax.js, and add these functions:
// Called on shopping cart update actions
function executeCartAction(obj)
{
// Display "Updating..." message
document.getElementById('updating').style.visibility = 'visible';
// Degrade to classical form submit if XMLHttpRequest is not available
if (!xmlHttp) return true;
// Save object reference
actionObject = obj;
// Initialize response and parameters
response = '';
params = '';
// If a link was clicked we get its href attribute
if (obj.tagName == 'A')
{
url = obj.href + '&AjaxRequest';
}
// If the form was submitted we get its elements
else
{
url = obj.action + '&AjaxRequest';
formElements = obj.getElementsByTagName('INPUT');
if (formElements)
{
for (i = 0; i < formElements.length; i++)
{
params += '&' + formElements[i].name + '=';
params += encodeURIComponent(formElements[i].value);
}
}
}
CHAPTER 13 ?– IMPLEMENTING AJAX FEATURES 427
// Try to connect to the server
try
{
// Make server request only if the XMLHttpRequest object isn't busy
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
{
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = cartActionStateChange;
xmlHttp.send(params);
}
}
catch (e)
{
// Handle error
handleError(e.toString());
}
// Stop classical form submit if AJAX action succeeded
return false;
}
// Function that retrieves the HTTP response
function cartActionStateChange()
{
// When readyState is 4, we also read the server response
if (xmlHttp.
Pages:
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543