/    Sign up×
Community /Pin to ProfileBookmark

How to return a variable on xmlhttp

Hi,

First of all I’m sorry if anybody has ask about this before, and sorry if the title is wrong.

So, I use a function to call a validation function, and if the validation is good, it will continue to submit the form. I use function like below :

[CODE]function validation(frm) {
var validate = returnTable(‘HELP’);
if(validate)
document.frmCompleteOrder.submit()
else
alert(‘Error’)
return false
}[/CODE]

The function returnTable() is a function that use an xmlhttp procedure.
On xmlhttp.onreadystatechange, it call the validateRow() function, which is the real validation function.
I make it that way because I need to push a list of table name into array then transfer it to the validateRow() function.

[CODE]function returnTable(pre)
{

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null) {
alert (“Your browser does not support AJAX!”);
return;
}

xmlhttp.open(“GET”,”totalTables.php?pref=”+ pre,true);
xmlhttp.send();

xmlhttp.onreadystatechange=function () {
var msg;

if (xmlhttp.readyState == 4) {
msg = validateRow(pre)
}

if(msg)
alert(msg);
return false;
};
}[/CODE]

With a code like this, the ‘validate’ in first code is always undefined, and it won’t submit the form. And I think it’s because the returnTable() function is not returning anything.

How can I return the ‘msg’ in :

[CODE]xmlhttp.onreadystatechange=function () {
var msg;

if (xmlhttp.readyState == 4) {
msg = validateRow(pre)
}

if(msg)
alert(msg);
return false;
};[/CODE]

to returnTable() function, so the returnTable() function can return the ‘msg’ into my first function.

Any suggestions and help are very appreciated.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@KorFeb 01.2011 — It depends on how and where did you call the function [B]validation()[/B]. It is called onclick? onsubmit? Where in the HTML code is placed the handler?
Copy linkTweet thisAlerts:
@marhen1985authorFeb 01.2011 — It depends on how and where did you call the function [B]validation()[/B]. It is called onclick? onsubmit? Where in the HTML code is placed the handler?[/QUOTE]The function called on button onclick..
[code=html]<input type="button" onclick="javascript:validation()" value=" Next >> "/>[/code]

It's placed at the bottom before </form> tag..
Copy linkTweet thisAlerts:
@KorFeb 01.2011 — Try:
<i>
</i>xmlhttp.onreadystatechange=function () {
var msg;
if (xmlhttp.readyState == 4&amp;&amp;xmlhttp.status==200) {
msg = validateRow(pre);
if(msg){
alert(msg);
return false;
}
}
}
Copy linkTweet thisAlerts:
@tuseroniFeb 01.2011 — kor, im not sure that will work. this issue here is that ajax is asynchronous, so an ajax call evaluates AFTER the function it was called from ends. so any function which depends on ajax will return undefined...

unless you do it synchronously.

change
[CODE]xmlhttp.open("GET","totalTables.php?pref="+ pre,true);
[/CODE]

to
[CODE]xmlhttp.open("GET","totalTables.php?pref="+ pre,false);
[/CODE]

then the code:
[CODE]
msg = validateRow(pre)

if(msg)
alert(msg);
return false;
[/CODE]

should evaluate after the connection and all the data is downloaded from the site. you wont need an onreadystatechange

though i dont know what validateRow is doing, nor the reason for this call (you dont do anything with xmlhttp.responseText or responseXML, so what is this call to the server DOING?)
Copy linkTweet thisAlerts:
@KorFeb 01.2011 — AJAX works asynchronous, but as long as you keep whichever function inside:
<i>
</i>if (xmlhttp.readyState == 4&amp;&amp;xmlhttp.status==200) {
// here the function
}

It will not be fired until the response comes back in one piece. Isn't it what you want?
Copy linkTweet thisAlerts:
@marhen1985authorFeb 02.2011 — kor, im not sure that will work. this issue here is that ajax is asynchronous, so an ajax call evaluates AFTER the function it was called from ends. so any function which depends on ajax will return undefined...

unless you do it synchronously.

change
[CODE]xmlhttp.open("GET","totalTables.php?pref="+ pre,true);
[/CODE]

to
[CODE]xmlhttp.open("GET","totalTables.php?pref="+ pre,false);
[/CODE]

then the code:
[CODE]
msg = validateRow(pre)

if(msg)
alert(msg);
return false;
[/CODE]

should evaluate after the connection and all the data is downloaded from the site. you wont need an onreadystatechange

though i dont know what validateRow is doing, nor the reason for this call (you dont do anything with xmlhttp.responseText or responseXML, so what is this call to the server DOING?)[/QUOTE]
The xmlhttp.responseText is processed in validateRow() function, the function is to retrieve all data from all floating tables in the form, then do the validation. If any field is empty it will give an error message which are saved in 'msg' variable, and return it to the returnTable() function.

I'm very sorry if I asked wrong, but my point is in the returnTable() function I can't retrieve the 'msg' variable. If I do like this :

[CODE]function returnTable(pre)
{

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null) {
alert ("Your browser does not support AJAX!");
return;
}

xmlhttp.open("GET","totalTables.php?pref="+ pre,true);
xmlhttp.send();

var msg;
xmlhttp.onreadystatechange=function () {
if (xmlhttp.readyState == 4) {
msg = validateRow(pre)
}
};

if(msg){
alert(msg);
return false;
}
}[/CODE]

The alert is don't show up..

And I want to return the 'msg', like :
[CODE]function returnTable(pre)
{

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null) {
alert ("Your browser does not support AJAX!");
return;
}

xmlhttp.open("GET","totalTables.php?pref="+ pre,true);
xmlhttp.send();

var msg;
xmlhttp.onreadystatechange=function () {
if (xmlhttp.readyState == 4) {
msg = validateRow(pre)
}
};

if(msg)
return msg;
}[/CODE]


So the validation() function in the form can retrieve the 'msg'. Something like :
[CODE]function validation() {
var validateMsg = returnTable('string');

if(validateMsg) {
alert('validateMsg')
return false
} else {
document.frmCompleteOrder.submit()
}
}[/CODE]
Copy linkTweet thisAlerts:
@marhen1985authorFeb 02.2011 — kor, im not sure that will work. this issue here is that ajax is asynchronous, so an ajax call evaluates AFTER the function it was called from ends. so any function which depends on ajax will return undefined...

unless you do it synchronously.

change
[CODE]xmlhttp.open("GET","totalTables.php?pref="+ pre,true);
[/CODE]

to
[CODE]xmlhttp.open("GET","totalTables.php?pref="+ pre,false);
[/CODE]

then the code:
[CODE]
msg = validateRow(pre)

if(msg)
alert(msg);
return false;
[/CODE]

should evaluate after the connection and all the data is downloaded from the site. you wont need an onreadystatechange

though i dont know what validateRow is doing, nor the reason for this call (you dont do anything with xmlhttp.responseText or responseXML, so what is this call to the server DOING?)[/QUOTE]
OMG.. I'm very sorry for my bad.

I do above suggestion and it works.. I just wanted to make my intention clear to you guys.. Final code is like this :
[CODE]function returnTable(pre)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null) {
alert ("Your browser does not support AJAX!");
return;
}
xmlhttp.open("GET","totalTables.php?pref="+ pre,false);
xmlhttp.send();

var msg;
msg = validateRow(pre)

if(msg) {
return msg;
}
}[/CODE]


And in the form :
[CODE]function validation(frm) {
var validateMsg = returnTable('string');

if(validateMsg) {
alert(validateMsg)
return false
} else {
document.frmCompleteOrder.submit()
}
}[/CODE]


Thank you for the helps...
×

Success!

Help @marhen1985 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.28,
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,
)...