Welcome to WebmasterWorld Guest from 54.162.155.183

Forum Moderators: open

Message Too Old, No Replies

AJAX query being sent but no result

     
12:00 am on Aug 25, 2009 (gmt 0)

5+ Year Member



Hi,

This should be really easy to fix, but I've been staring at it all day.

function imageUpdater(action,image,expiration) {
var x_statement = "managecontent2.php?action=" + action + "&image_id=" + image + "&expiration=" + encodeURIComponent(expiration);
alert(x_statement);
xmlHttp = GetXmlHttpObject()
xmlHttp.open("GET",x_statement,true);
xmlHttp.send(null);
if (xmlHttp.readyState==4) {
if (xmlHttp.status == 200) {
var response = xmlHttp.responseText;
alert(response);
var theID = "image_" + image;
document.getElementById(theID).display = none;
}
}
}

The x_statement is indeed sent, and the php does receive it and process it. The php is supposed to send a "1" back to the javascript if everything went correctly but I can't get that simple result to alert. In fact, *nothing* alerts. Ever. Am I confusing a variable name somewhere?

Thanks for the help.

Glenn

2:13 am on Aug 25, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



xmlHttp.open("GET",x_statement,true)

This indicates an asynch request, in which case you will need to wait for reply (onReadyStateChange).

Changing to false (synch), should work, but will freeze interface while response pending.

6:39 am on Aug 25, 2009 (gmt 0)

5+ Year Member



You have not assigned a function to the xmlHttp object's onreadystatechange event, and some other small errors in there too. The below should probably fix it:
function imageUpdater(action,image,expiration) {
var x_statement = "managecontent2.php?action=" + action + "&image_id=" + image + "&expiration=" + encodeURIComponent(expiration);
alert(x_statement);
xmlHttp = GetXmlHttpObject();
xmlHttp.onreadystatechange = function () { //assign a function to handle the response, before open and send...
//we can use the 'this' keyword within the onreadystatechange function,
//will refer to the xmlHttp object it's self
if (this.readyState == 4 && this.status == 200) {
var response = this.responseText;
alert(response);
var theID = "image_" + image;
//it's document.getElementById NOT document.getElementByID, case sensitive
document.getElementById(theID).style.display = 'none'; //style.display, not just .display,
//and 'none' is supposed to be string, unless you have a variable by that name somewheres...
}
};
xmlHttp.open("GET",x_statement,true);
xmlHttp.send(null);
}
6:43 am on Aug 25, 2009 (gmt 0)

5+ Year Member



nevermind about the document.getElementByID note, must have been seeing things here... or copy and paste error into my editor... or aliens...
4:10 pm on Aug 25, 2009 (gmt 0)

5+ Year Member



Thanks, folks.

I changed the true to false because I really don't need asynchronous. I also corrected the dhtml typos.

Alles gut.

Glenn

 

Featured Threads

Hot Threads This Week

Hot Threads This Month