homepage Welcome to WebmasterWorld Guest from 54.226.18.74
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
AJAX query being sent but no result
pillsbur




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

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

 

daveVk




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

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.

astupidname




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

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);
}

astupidname




msg:3978140
 6:43 am on Aug 25, 2009 (gmt 0)

nevermind about the document.getElementByID note, must have been seeing things here... or copy and paste error into my editor... or aliens...

pillsbur




msg:3978337
 4:10 pm on Aug 25, 2009 (gmt 0)

Thanks, folks.

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

Alles gut.

Glenn

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved