Hey there DaniWeb, Recently i found out that there was a thing that didn't work in my CMS.

Code (index.php):

<body onLoad=\"javascript:loadManageNews();loadManageNav();loadMCE();\"> (in a PHP file)

Issue: It calls the loadManageNews() function just finse but it doesn't call the loadManageNav(). It also loads the loadMCE() function just fine. And yes there is a function named loadManageNav() and the ajax.js file is included.

Code (ajax.js):

function refresh() {
    window.location = 'index.php';
}
function update() {
    document.getElementById("answer").innerHTML="<img src='./images/updating.gif' />";
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
     }
    else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
		    setTimeout('refresh()',1000);
            document.getElementById("answer").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST","./process/update.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("version=" + document.getElementById("version").value + "&version1=" + document.getElementById("version1").value);
}
function loadManageNews() {
    document.getElementById("manage").innerHTML='Loading...';
	if (window.XMLHttpRequest)
	  {
	  xmlhttp=new XMLHttpRequest();
	  xmlhttp.open("GET","./process/managen.php",false);
	  xmlhttp.send(null);
	  }
	else
	  {
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  xmlhttp.open("GET",url,false);
	 
	  xmlhttp.send();
	  }
	document.getElementById("manage").innerHTML=xmlhttp.responseText;
	xmlhttp = null;
}
function loadManageNav() {
    document.getElementById("managenav").innerHTML='Loading...';
	if (window.XMLHttpRequest)
	  {
	  xmlhttp=new XMLHttpRequest();
	  xmlhttp.open("GET","./process/managenav.php",false);
	  xmlhttp.send(null);
	  }
	else
	  {
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  xmlhttp.open("GET",url,false);
	 
	  xmlhttp.send();
	  }
	document.getElementById("managenav").innerHTML=xmlhttp.responseText;
	xmlhttp = null;
}

function delNews(id) {
	confirm = confirm("You are just about to delete the news post with id " + id + ".");
	if(confirm) {
        if (window.XMLHttpRequest) {
            xmlhttp=new XMLHttpRequest();
        }
        else {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                    wackoo("./process/managen.php","manage");
            }
        }
        xmlhttp.open("GET","./process/deleten.php?id=" + id,true);
        xmlhttp.send();
	}
	else {
	}
	delete confirm;
}

there you go...
and yes the ./process/managenav.php file exists and it outputs just fine if i go to the file alone.

Please help.

nadskap2,

Several things jump off the page.

Function update has a 1 second (1000 ms) timeout before giving up and refreshing the page. This seems unnecessarily short even for fast intranet scenarions (though maybe ok for testing with server on local machine).

Functions loadManageNews and loadManageNav both use url , which is undefined.

Functions loadManageNews and loadManageNav both attempt synchronous response handling. This is not recommended, even in browsers that do it (IE). Always handle AJAX resposes asynchronously as per your upload function.

You will save a lot of lines of code by creating a general puspose getXmlHttpObject function, something like this:

function getXmlHttpObject() {
    if (window.XMLHttpRequest){
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}

Your other functions should then look something like this:

function loadManageNav() {
    var el = document.getElementById("managenav");
    if(el) {
        var xmlhttp = getXmlHttpObject();
        if(xmlhttp) {
            el.innerHTML = 'Loading...';
            function abort(){
                xmlhttp.abort();
                el.innerHTML = 'Timeout';
            }
            var t = setTimeout(function(){ abort(); }, 20000);//eg 20 seconds
            xmlhttp.open("GET", "./process/managenav.php", true);
            xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState==4) {
                    clearTimeout(t);
                    if (xmlhttp.status==200) { el.innerHTML = xmlhttp.responseText; }
                    else { el.innerHTML = 'Error serving data'; }
                }
            }
            xmlhttp.send(null);
        }
        else {
            el.innerHTML = 'Error requesting data.';
        }
    }
}

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.