/    Sign up×
Community /Pin to ProfileBookmark

AJAX returning a variable

I want to return true if the responseText is nothing and return false if the responseText is something. These return statements must be at the end of the function checkFields()….I’ve tried a million things and couldn’t find any way to determine if the ajax request returned a value or not (at least I couldn’t determine this from the checkFields function, where I need to be able to determine it). Does anybody know a way to determine if the request returned anything from the checkFields function? Thanks for any help.

[code]
function checkfields() {
xmlHttp=GetXmlHttpObject();

if(xmlHttp==null) {
return false;
}

tinyMCE.triggerSave();
title = document.getElementById(“theTitle”).value;
post = document.getElementById(“thePost”).value;
attach = document.getElementById(“attach”).value;

var url = “http://localhost/panl/scripts/forum/newpost.php”;
var params = “title=”+title+”&post=”+post+”&attach=”+attach+”&ajax=true”;

xmlHttp.open(“POST”, url, true);

//Send the proper header information along with the request
xmlHttp.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
xmlHttp.setRequestHeader(“Content-length”, params.length);
xmlHttp.setRequestHeader(“Connection”, “close”);
xmlHttp.onreadystatechange = stateChangedNewPost;
xmlHttp.send(params);

return false;
}

function stateChangedNewPost() {
thisElement = getThisElement();

if(xmlHttp.readyState==4) {
thisElement.innerHTML = xmlHttp.responseText;
}

}

function getThisElement() {

for (i=0;i<document.getElementsByTagName(“div”).length; i++) {
if(document.getElementsByTagName(“div”).item(i).className == “row”){
if(document.getElementsByTagName(“div”).item(i).title == “error”) {
return thisElement = document.getElementsByTagName(“div”).item(i);
}
}
}

}
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@youngharris100Mar 27.2007 — it doesen't work because you're stupid.
Copy linkTweet thisAlerts:
@rootMar 27.2007 — return !xmlHttp.responseText ? true : false;

I think.
Copy linkTweet thisAlerts:
@aj_nscauthorMar 27.2007 — Yeah that works fine, but not from the function checkfields().... it only works in the function stateChangedNewPost which is called onreadystatechange......the problem is I can't get a return variable which determines if there was responseText or not in the checkfields function.....alas...back to the drawing board I guess.
Copy linkTweet thisAlerts:
@rootMar 28.2007 — Use an external variable and run an event timer that calls a function to check the value change, if any.
Copy linkTweet thisAlerts:
@aj_nscauthorMar 28.2007 — Thanks a bunch. However, I'm more of a PHP programmer than a Javascript guy, could you give me an example? I'd be grateful, thanks.
Copy linkTweet thisAlerts:
@rootMar 29.2007 — Sorry I dont, this site has, you need to look for them.
×

Success!

Help @aj_nsc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...