action + '&AjaxRequest';
params = '';
// obtain selected attributes
formSelects = form.getElementsByTagName('SELECT');
if (formSelects)
{
for (i = 0; i < formSelects.length; i++)
{
params += '&' + formSelects[i].name + '=';
selected_index = formSelects[i].selectedIndex;
params += encodeURIComponent(formSelects[i][selected_index].text);
}
}
// Try to connect to the server
try
{
// Continue only if the XMLHttpRequest object isn't busy
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
{
// Make a server request to validate the extracted data
xmlHttp.open("POST", request, true);
xmlHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = addToCartStateChange;
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 addToCartStateChange()
{
// When readyState is 4, we also read the server response
if (xmlHttp.readyState == 4)
{
// Continue only if HTTP status is "OK"
CHAPTER 13 ?– IMPLEMENTING AJAX FEATURES 417
if (xmlHttp.status == 200)
{
try
{
updateCartSummary();
}
catch (e)
{
handleError(e.toString());
}
}
else
{
handleError(xmlHttp.statusText);
}
}
}
// Process server's response
function updateCartSummary()
{
// Read the response
response = xmlHttp.
Pages:
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533