////////////////////////////////////////////////// // AJAX POST Functions v1.8 [11-24-09] // ////////////////////////////////////////////////// /* This AJAX library is used by writing a single function which specifies what information is to be sent to the server and where that information should go when the server is done. This AJAX library is called by specifying: 1) The URL of the server side page which will perform the calculations 2) The Parameters/values which are being sent to the server 3) Where the information from the server is to be sent 4) What kind of target location the information is to be sent to 0 == .innerHTML (such as a
or tag) 1 == .value (such as tag) 2 == an alert 3 == write to a window 4 == return NOTHING 5 == return the value - I can't get this to work!!! 6 == run specified JS 5) What kind of specific message should be displayed if there is an error 6) What kind of wait message should be displayed during the calculation Example: function basicAJAX(a,b) { var poststr="?&a="+escape(a)+"&b="+escape(b); makePOSTRequest('php/calculator.php', poststr,'ajaxtarget',0,"There was an error.","Please be patient while we calculate."); } Ver 1.8 Added support for running JS Ver 1.7 ? Ver 1.6 Added #4 - no return of a value to the javascript Ver 1.5 Added "AJAXready = true;" and a check to see if AJAXready is true/false. Upon request, it is set to false. Upon completion of a request, it is set to true again. Added "http_request.abort();" if a new request comes in before the last one is done. Added error message for AJAXtype #3 (writing to a window) */ var http_request = false; var AJAXin = ''; var AJAXtype = ''; var AJAXerror = ''; var AJAXready = true; function makePOSTRequest(url, parameters, ajaxtarget, ajaxtype, ajaxerror, waitmessage) { if (waitmessage != '' && waitmessage != null) { if (ajaxtype == 0) { document.getElementById(ajaxtarget).innerHTML = waitmessage; } if (ajaxtype == 1) { document.getElementById(ajaxtarget).value = waitmessage; } if (ajaxtype == 3) { ajaxtarget.document.write(waitmessage); ajaxtarget.document.close(); } } else { if (ajaxtype == 0) { x = ''; document.getElementById(ajaxtarget).innerHTML = x; } if (ajaxtype == 1) { document.getElementById(ajaxtarget).value = 'Calculating...'; } if (ajaxtype == 3) { x = ''; ajaxtarget.document.write(x); ajaxtarget.document.close(); } if (ajaxtype == 6) { x = ''; document.getElementById(ajaxtarget).innerHTML = x; } } AJAXin = ajaxtarget; AJAXtype = ajaxtype; http_request = false; AJAXerror = ajaxerror; if (window.XMLHttpRequest) { // Mozilla, Safari,... http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { http_request.overrideMimeType('text/html'); } // set type accordingly to anticipated content type // http_request.overrideMimeType('text/xml'); } else if (window.ActiveXObject) { try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } // IE catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } } if (!http_request) { alert('Cannot create XMLHTTP instance'); return false; } if (AJAXready) { AJAXready=false; http_request.onreadystatechange = alertContents; 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); } else { http_request.abort(); AJAXready=true; makePOSTRequest(url, parameters, ajaxtarget, ajaxtype, ajaxerror, waitmessage); } } function alertContents() { if (http_request.readyState == 4) { if (http_request.status == 200) { if ( escape(http_request.responseText)=="dontfencemein%09" ) { var newtoyou = window.open("http://www.pdrater.com",'newtoyou','height=600,width=850'); } else { if (AJAXtype == 0) { document.getElementById( AJAXin ).innerHTML = http_request.responseText; } else if (AJAXtype == 1) { document.getElementById( AJAXin ).value = http_request.responseText; } else if (AJAXtype == 2) { alert(http_request.responseText); } else if (AJAXtype == 3) { AJAXin.document.write(http_request.responseText); AJAXin.document.close(); } else if (AJAXtype == 4) { } // else if (AJAXtype == 5) { return http_request.responseText; } else if (AJAXtype == 6) { eval(http_request.responseText); } } } else { if (AJAXerror == '') { AJAXerror = 'There was a problem with the request.'; } if (AJAXtype == 0) { document.getElementById( AJAXin ).innerHTML = AJAXerror; } else if (AJAXtype == 1) { document.getElementById( AJAXin ).value = AJAXerror; } else if (AJAXtype == 2) { alert(AJAXerror); } else if (AJAXtype == 3) { AJAXin.document.write(AJAXerror); AJAXin.document.close(); } // else if (AJAXtype == 5) { return AJAXerror; } } AJAXready = true; } }