returning the XML result to a returning function

When I call compexists and alert the response text it displays it correctly, however it displays "undefined" in the validate_transfer function.  I'm not sure what I'm doing wrong here.  I'm using IE 7
function validate_transfer(formname,dialid)
{
	var dupname = compexists(formname.name.value,dialid);
	alert(dupname);
}
 
function compexists(compname,dial_id)
{
    var xmlHttp = getXMLHttp();
    xmlHttp.onreadystatechange = function()
      {
         if(xmlHttp.readyState == 4)
             {
                 alert(xmlHttp.responseText);
                 return xmlHttp.responseText;				
             }
      }
xmlHttp.open("GET", "compexists.php?compname="+compname+"&dialplan_id="+dial_id, true); 
xmlHttp.send(null);	
}

Open in new window

ddsexpertsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wassa_rCommented:
The function compexists doesn't actually return anything. The function onreadystatechange does, see the difference? You'll have to change the onreadystatechange function to actually call part 2 of your validate_transfer();

You'll do something similar to:

validate_transfer(formname, dialid);

change the readystatechange function to

 if(xmlHttp.readyState == 4)
             {
                 
                 valid_transfer2(compname,dial_id,xmlHttp.responsetext);
                                         
             }

then do whatever you want in part 2 with the response.
0
cyberhiteshCommented:
Ajax call is asynchronous, so it goes into background and control gets transferred to the alert() which shows undefined because it does not contains any data.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Applications

From novice to tech pro — start learning today.