Hey guys, I have come unstuck... cost...

I started using Ajax yesterday and have been pleasantly surprised by how easy to use it is...

What Ive done is build a crude instant messenger, which people need to log into... however, because of how I intend on using it... I want to be able to log people out when they close the browser (or a tab). So I thought I could use

window.onunload = removeLoggedIn();

Which will call a function which passes the user's username to a php script which deletes him out of the 'logged in' table... and thus he is not shown as online anymore...

So I tested my function by sticking an alert into it, and when I refresh the page - it fires... (It doesnt when you close the page, but maybe that isnt a problem as such).

Does anyone know why the function called upon onunload isn't passing the goods to my php script?

function removeLoggedIn(){
	var http;
	http=GetXmlHttpObject(); //create a valid xmlhttprequest
	
        //get username, password to apss to php script
	var userName = document.forms["loginForm"]["whoisit"].value;
	var pword = document.forms["loginForm"]["pword"].value;
	
       //php script details
	var url = "logout.php";
	var params = "uname=" + userName + "&pword=" + pword;
	http.open("POST", url, true);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	http.send(params);
}
window.onunload = removeLoggedIn();

Should Be

window.onunload = function () {
  removeLoggedIn();
};
// OR
window.onunload = removeLoggedIn;

You don't want to assign the result of removeLoggedIn to the onunload, you just want to assign the function to it.

Hi

Thanks for the input, but doesnt seem to have sorted this out, but I think I have tracked down the issue... which for some reason I cant solve... though it seems like it should be easy.

function removeLoggedIn(){
	
	var http;
	http=GetXmlHttpObject();

	var url = "logout.php";
//	var userName = document.forms["loginForm"]["whoisit"].value;
//	var pword = document.forms["loginForm"]["pword"].value;
	var params = "uname=rogdodge&pword=wrench";
//	var params = "uname=" + userName + "&pword=" + pword;
	http.open("POST", url, true);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	http.send(params);
}

The info Im passing to the PHP script is a username and password, which is coming out of two text fields... I don't think my script is finding this info - but its odd because that 'path' (document.forms etc...) is used in other scripts and works just fine... If I hard code the username and password into the script, everything gets to the PHP and all works fine...

Ive tried putting an alert into the removeLoggedIn function, to see if its finding those details and it isnt...

Sorry, meant to say... If I use

window.onload = removeLoggedIn;

The hardcoded values dont even get submitted....?!

Hi,

try to put some query mark on your defined url variable : var url = "logout.php[b]?[/b]"; and also bring this up, right after the declaration of your http ( HttpRequest object ).
On line#4 (( http.overrideMimeType ) ? http.overrideMimeType("text/xml") : http );

Hi

After a lot of fiddling and frustration I created a new file, with the script and that seems to have solved the problem...

I suspect there was a conflict somewhere, even though I cant work out where, there were no global variables at play etc, and so each function should have been secured in itself... anyway, its working!

Thanks for your input everyone,

lworks

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.