PDA

View Full Version : Need some help with passing some commands into a function.



bennyy007
10-19-2009, 12:56 PM
Hello,

I got this script that I coded up last night but for some reason whenever i passed stuff into the function, it comes up as 'undefined' I've had issues like this before but can't remember how i solved it the XMLHttpRequest object works but it relies on these inputs to perform the correct commands back to my PHP file that sends back the requested data.

So example of how I'm passing the commands in:

getAjax('getallmessages','inline_chat_win');

the first command will be used to pass to the php file and the second is the DIV element to be used in:

$(''+ div +'').html(httpReq.responseText).fadeIn(1000);

But neither get the correct input as both just get undefined? This has baffled me as I've tried doing many things to solve it.... here's my function below:



function getAjax(cmd,frame) {
var httpReq;

try{ // Firefox, Opera 8.0+, Safari
httpReq = new XMLHttpRequest();
} catch (e) {
try{ // Microsoft Internet Explorer (ActiveX)
httpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{ // Again with Microsoft => Boring!
httpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// Oh dear their browser doesn't support this? This is very rare!
alert("Your browser seems to not support the XMLHttpRequest object? Please consider upgrading your browser.");
return false;
}
}
}

httpReq.onreadystatechange = function() {
// Remember to also set 200 for the status
if( httpReq.readyState == 4 && httpReq.status == 200 ) {
$('#'+ frame +'').html(httpReq.responseText).fadeIn(1000);
}
}
var command = cmd;
httpReq.open("GET","inline_chat.php?cmd_="+ command,true);
httpReq.send(null);
}

window.onload = function() {
setInterval("getAjax()",10000);
}


and heres the code block that sends in the input:



window.onload = function() {
$("#loading").remove();
getAjax('getallmsgs','inline_win_chat'); // Gets all messages from the server
}

jscheuer1
10-19-2009, 03:24 PM
I see two window.onload:



window.onload = function() {
setInterval("getAjax()",10000);
}

and:



window.onload = function() {
$("#loading").remove();
getAjax('getallmsgs','inline_win_chat'); // Gets all messages from the server
}

There can be only one on a page, and whichever is parsed last by the browser will be the one that's used. If that happens to be the one with no parameters passed to the function, those parameters will be undefined.

bennyy007
10-19-2009, 03:33 PM
Thank you :) that did the trick i feel really stupid for making that mistake, again was tired when i did that part. But thanks again!!!