Hello Everyone,

I have a "small" problem with java. :-)

I made a Php - Javascipt for displaying the status of my servers.

The Xml response and the php scipt are working fine.
But the Javascipt doesn't do what i want it to do.

I Have 2 Divs in my html (id="result1" and id id="result2"), but my javascipt is overwriting the result from "result1" with the one from "result 2".

Firebug is also showing 2 ajax requests and resonses, but i am only seeing 1 of them on my screen. (Without any Errors in firebug).

Demo => http://a000008.pixelstudio.eu.com/index.php?pid=3
Html Source code => Go to demo (View source)
Javascript Source =>

//LOAD xmlHttp method.
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}

//PARSE DATA
function setmessage(value,org) {
var messageArea = document.getElementById(org + "result");
var fontcolor = "red";
if ( value == 1 ) {
var fontcolor = "green";
var msg = "ONLINE";
} else {
var fontcolor = "red";
var msg = value;
}
messageArea.innerHTML = "<font color=" + fontcolor + ">" + msg + "</font>";
}

//RECIEVE DATA TRUE XML
function callback() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var value = xmlHttp.responseXML.getElementsByTagName("msg")[0].firstChild.data;
var org = xmlHttp.responseXML.getElementsByTagName("org")[0].firstChild.data;
setmessage(value,org);
}
}
}

//PUSH DATA
function ping_service(value) {
createXMLHttpRequest();
var data = "update.php?id=" + escape(value);
xmlHttp.open("GET", data, true);
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);
}

What am i doing wrong ?

Thanks Ilias

Ps: Sorry for my English (Dutch speaking)

You have two divs, id="result":

<div id="result">

ID names must be unique and your div tags are open. I don't see the ID's you say.


Matti Ressler
Suomedia

Hey,

Thanks for the reply. I added the id tag to the div result and changed my java code to make it "work". But the <div id="result2"></div> is till getting overwritten by <div id="result1"></div>. Even with an unique id attached to it.

Getting al little "koekoe" if you know what i mean :)

Ps: Javascript value org has been changed to resultId

Problem Fixed, Changed the XmlRequest to a function.

//LOAD xmlHttp method.
var xmlhttp

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp = false
	}
}

function createXMLHttpRequest() {
	var xmlhttplocal;
	try {
		xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
		} catch (e) {
			try {
				xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
			} catch (E) {
				xmlhttplocal=false;
			}
		}
	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
		try {
			var xmlhttplocal = new XMLHttpRequest();
		} catch (e) {
			var xmlhttplocal=false;
			alert('couldn\'t create xmlhttp object');
		}
	}
	return(xmlhttplocal);
}

//PARSE DATA
function setmessage(value,org) {
	var messageArea = document.getElementById("result" + org);
	var fontcolor = "red";
	if ( value == 1 ) {
		var fontcolor = "green";
		var msg = "ONLINE";
	} else {
		var fontcolor = "red";
		var msg = value;
	}
	messageArea.innerHTML = "<font color=" + fontcolor + ">" + msg + "</font>";
}

//RECIEVE DATA TRUE XML
function callback() {
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			var value = xmlhttp.responseXML.getElementsByTagName("msg")[0].firstChild.data;
			var org = xmlhttp.responseXML.getElementsByTagName("resultId")[0].firstChild.data;
			setmessage(value,org);
		}
	}
}

//PUSH DATA
function ping_service(value) {
	createXMLHttpRequest();
	var data = "update.php?id=" + escape(value);
	xmlhttp.open("GET", data);
	xmlhttp.onreadystatechange = callback;
	xmlhttp.send(null);
}

When I look at your page I only see these errors:

Error: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]"  nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)"  location: "JS frame :: http://a000008.pixelstudio.eu.com/update.js :: callback :: line 51"  data: no]
Source File: http://a000008.pixelstudio.eu.com/update.js
Line: 51

Error: uncaught exception: [Exception... "Component returned failure code: 0xc1f30001 (NS_ERROR_NOT_INITIALIZED) [nsIXMLHttpRequest.send]"  nsresult: "0xc1f30001 (NS_ERROR_NOT_INITIALIZED)"  location: "JS frame :: http://a000008.pixelstudio.eu.com/update.js :: ping_service :: line 65"  data: no]

Matti Ressler
Suomedia

already fixed (Was playing around), code working in FireFox now, But not in IE 7.

No errors now. I only can see one response "Online". Template is broken.

Matti Ressler
Suomedia

OK, Normally you should see 2 "ONLINE" signs => id=result1 and id=result2

But he is only displaying one of them (Infact they are overwrtiting themself)

Thanks for the help

This article has been dead for over six months. Start a new discussion instead.