topical media & game development

talk show tell print

basic-ajax-08-Cart.js / js



  
  
  var xHRObject = false;
  
  if (window.ActiveXObject)
  {
  xHRObject = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest)
  {
  xHRObject = new XMLHttpRequest();
  }
  
  function getData()
  {
      if ((xHRObject.readyState == 4) &&(xHRObject.status == 200))
      {
          var serverResponse = xHRObject.responseXML;
          var header = serverResponse.getElementsByTagName("book");
          var spantag = document.getElementById("cart");
          spantag.innerHTML = "";
          for (i=0; i<header.length; i++)
          {
             
          if (window.ActiveXObject)
          {
               spantag.innerHTML += " " +header[0].firstChild.text;
               spantag.innerHTML += " " + header[0].lastChild.text + " " + "<a href='javascript:AddRemoveItem(\"Remove\");'>Remove Item</a>";
          }
          else
          {
               spantag.innerHTML += " " +header[0].firstChild.textContent;
               spantag.innerHTML += " " + header[0].lastChild.textContent + " " + "<a href='javascript:AddRemoveItem(\"Remove\");'>Remove Item</a>";
          }
         }
      }
  }
  
  function getBody()
  {
       var argument = "book=";
       argument += encodeURI(book.innerHTML); 
       argument += "&ISBN=";
       argument += encodeURI(ISBN.innerHTML); 
       argument += "&authors=";
       argument += encodeURI(authors.innerHTML); 
       argument += "&price=";
       argument += encodeURI(price.innerHTML); 
       return argument;
  }
  
  function AddRemoveItem(action)
  {
            var book  = document.getElementById("book").innerHTML;
            var num = (Math.round((Math.random()*999999)+1));
             var newform = document.forms[0];
  var bodyofform = getBody(newform); 
             
            if(action=="Add")
            {
            xHRObject.open("POST", newform.action, true);
            xHRObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xHRObject.onreadystatechange = getData;
  xHRObject.send(bodyofform); 
              
            }
            else
            {
             xHRObject.open("POST", newform.action, true);
  xHRObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xHRObject.onreadystatechange = getData;
  xHRObject.send(bodyofform); 
            }
           
  }
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.