gimecoffee
MIS
- Mar 3, 2006
- 25
My Ajax routine runs a dozen times (or so) with no problem and then stops all internet connections. Javascript is still working since I have some js popup windows that still activate but my ajax routine (saving data to the server) stops working and I can't post/get a form to the server. I read something about multiple ajax requests per page causing a race condition (or something like that) if I'm using global variables instead of local. I think I've got all local variables now but I'm still having the problem. Maybe one of you can tell me what I'm doing wrong. Doesn't seem to be a problem in Firefox.
ajaxRequest(f,pairs,func) starts the routine. f is the form, pairs I leave blank usually(an in this case specifically) so that getFormValues() will just send all values of the form to the server, and func is the PHP function I want the server to run on the current form data. PHP may or may not send anything back.
Tusan Tuk
Rois
ajaxRequest(f,pairs,func) starts the routine. f is the form, pairs I leave blank usually(an in this case specifically) so that getFormValues() will just send all values of the form to the server, and func is the PHP function I want the server to run on the current form data. PHP may or may not send anything back.
Code:
function ajaxRequest(f,pairs,func) {
var file = '/index.php';
var SID, str;
var http_request = false;
if(func != "") {
func = "dsup="+func+"();&";
}
if(pairs=="") {
str = "ajax=true&"+func+getFormValues(f,"");
} else {
if(sid) {
SID = sid+'&'
} else if (document.getElementById('PHPSESSID')) {
SID = "PHPSESSID="+document.getElementById('PHPSESSID').value+"&";
}
str = "ajax=true&"+SID+func+pairs;
}
if (typeof window.ActiveXObject != 'undefined' ) {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} else {
http_request = new XMLHttpRequest();
}
http_request.open( "POST", file, true );
http_request.setRequestHeader("Content-Type","application/x-[URL unfurl="true"]www-form-urlencoded;[/URL] charset=UTF-8");
http_request.onreadystatechange = function() { if (window.ajaxUpdate)ajaxUpdate(http_request); };
http_request.send(str);
}
function ajaxUpdate(http_request) {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
var response = http_request.responseText.split("|");
if (response[0]!="") {
eval(response[0]);
}
} else {
alert("Status code " + http_request.status+"\n"+
"Server connection appears to be\n"+
"down. Try re-entering your request\n"+
"or try again later.");
}
}
}
function getFormValues(fobj,valFunc) {
var str = "";
var valueArr = null;
var val = "";
var cmd = "";
for(var i = 0;i < fobj.elements.length;i++) {
switch(fobj.elements[i].type) {
case "text":
if(valFunc) {
cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
val = eval(cmd)
}
str += fobj.elements[i].name +
"=" + escape(fobj.elements[i].value) + "&";
break;
case "checkbox":
if(fobj.elements[i].checked) {
str += fobj.elements[i].name +
"=" + escape(fobj.elements[i].value) + "&";}
break;
case "hidden":
if(valFunc) {
cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
val = eval(cmd)
}
if(fobj.elements[i].name!='dsup'){
str += fobj.elements[i].name +
"=" + escape(fobj.elements[i].value) + "&";}
break;
case "select-one":
str += fobj.elements[i].name +
"=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
break;
}
}
str = str.substr(0,(str.length - 1));
return str;
}
Tusan Tuk
Rois