Seperate Div refresh in the same function.
Okay so, I found the code below and adapted it so it could be used on multiple pages; login, registration, etc.
What I'm making is a game, and on a certain page areas of the page will need to be reloaded. As soon as I added a new parameter (div) into the functions the alertContents doesn't seem to work.
The functions work like this:
In the html form code the get function collects the information on the form, then, depending on the page parameter, the switch statement collects the forms code for the post information. The post information is then passed into the post function where the function determines the browser that is being used, then set the function to run when the http request state is changed. This is the point where I believe it to be broken. From there the alertContents function outputs the result of the processing from the server.
code:
Code:
var http_request = false;
function makePOSTRequest(url, parameters, div)
{
http_request = false;
if (window.XMLHttpRequest)
{ // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
}
else if (window.ActiveXObject)
{ // IE
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
}
}
}
if (!http_request)
{
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents(div);
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);
}
function alertContents(div)
{
if (http_request.readyState == 4)
{
if (http_request.status == 200)
{
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById(div).innerHTML = result;
}
else
{
alert('There was a problem with the request.');
}
}
}
function get(obj, page, div)
{
switch (page)
{
case 'login.php':
var poststr = "un=" + escape(encodeURI( document.getElementById("un").value )) +
"&pw=" + escape(encodeURI( document.getElementById("pw").value )) +
"&login=" + escape(encodeURI( document.getElementById("login").value ));
makePOSTRequest(page, poststr, div);
refresh();
break;
case 'register.php':
var poststr = "un=" + escape(encodeURI( document.getElementById("un").value )) +
"&pw=" + escape(encodeURI( document.getElementById("pw").value )) +
"&cpw=" + escape(encodeURI( document.getElementById("cpw").value )) +
"&em=" + escape(encodeURI( document.getElementById("em").value )) +
"®ister=" + escape(encodeURI( document.getElementById("register").value ));
makePOSTRequest(page, poststr, div);
break;
case 'chat_insert.php':
var poststr = "msg=" + escape(encodeURI( document.getElementById("msg").value ));
makePOSTRequest(page, poststr, div);
break;
case 'home.php':
var poststr = "logout=" + escape(encodeURI( document.getElementById("logout").value ));
makePOSTRequest(page, poststr, div);
refresh();
break;
}
}
Re: Seperate Div refresh in the same function.
Have you tried debugging it yet with Firefox's Error Console? I suggest you do that first.
Re: Seperate Div refresh in the same function.
I don't use firefox, but i'll download it and give it a go. Thanks
Re: Seperate Div refresh in the same function.
You should have Internet Explorer, Firefox, and Safari for testing at all times. Firefox's Error Console is a great debugging tool. Also, you should look into Firebug for debugging as well.
Re: Seperate Div refresh in the same function.
And if you want to make sure, you should have more than one machine to get a good overview of different versions of IE and to a lesser extent FF.
I'd also just like to add that I prefer Chrome's debugger (but each to their own; it's mainly because I use my C-style include method for complex website scripts, and Chrome loads and runs it that bit quicker...)
Re: Seperate Div refresh in the same function.
Yeah I use chrome, I used to use firefox before chrome came out.
Where is chromes debugger?
Re: Seperate Div refresh in the same function.
Right click -> Inspect Element. Somewhere on the bottom bar of that window, there's the console. It's useful, because if you open it, and hen reload the page (it's tied to the process from which you open it) it also stores a copy of all the resource files, so it'll show you on which line errors occur.
Re: Seperate Div refresh in the same function.
Ahh yes, i've used the inspect element but didn't notice the console before. Thanks, i'll check it out