View Single Post
Old 06-06-2007, 01:41 AM   PM User | #9
djmonkey1
New Coder

 
Join Date: Mar 2006
Location: In an apartment.
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
djmonkey1 is an unknown quantity at this point
Here is the code for the zip code box:

Code:
<input name="update_delivery_postcode" size="5" value="91932" onChange="updateShippingZone('delivery_postcode', encodeURIComponent(this.value), this.form)">
So if a change is made to the zip updateShippingZone is called:
Code:
function updateShippingZone(field, value, data) {

    updateOrdersField(field, value);
	
		    if (confirm('You have changed some shipping information. Would you like to recalculate the order totals?')) {
			
			  obtainTotals(data);
		    
			}

}//end function updateShippingZone(field, value) {
I'm going to skip updateOrdersField for now. obtainTotals we have already seen, this is where the data is looped:
Code:
  function obtainTotals(data) { //this is used for processing/updating order totals
    
    // Set up data variable
    var formdata = "";
	 
    // Loop through form fields
    for (i=0; i < data.length; i++)
    {
         //Build Send String
         formdata = formdata + data.elements[i].name + "=" + escape(data.elements[i].value) + "&";
         
    }
		  makePOSTRequest('filename', formdata, 'totals');
  }//end function obtainTotals(obj) {
Then we go to makePOSTRequest and alertTotals or alertComments (I'm going to condense some of these functions down once I get this functioning properly):
Code:
   var http_request = false;
   function makePOSTRequest(url, parameters, div) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      if (div == 'comments') {http_request.onreadystatechange = alertComments;}
	  if (div == 'totals') {http_request.onreadystatechange = alertTotals;}
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertComments() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            document.getElementById("commentsBlock").innerHTML = http_request.responseText;  
			         
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   
      function alertTotals() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            document.getElementById("totalsBlock").innerHTML = http_request.responseText;  
			         
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
At this point the data from filename is loaded into the browser in the appropriate place. I've compared HTML from the original and regenerated data and have found no differences in structure or content.
djmonkey1 is offline   Reply With Quote