PDA

View Full Version : Reference output from Basic Ajax Routine in javascript?



hanji
02-02-2009, 09:17 PM
1) Script Title: Basic Ajax Routine

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex17/ajaxroutine.htm?agefield=32

3) Describe problem: I'm trying to reference a value returned from the script it's sending to. Basically, I want to do a hostname lookup via a PHP script. The script will return a 1 or 0, depending if the hostname is in DNS or not.

I want to be able to reference that value on the javascript side after the getting the value for a client side error.

Here is basically what I'm trying to do...


function processGetPost(){
var myajax=ajaxpack.ajaxobj
var myfiletype=ajaxpack.filetype
if (myajax.readyState == 4){ //if request of file completed
if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
//if (myfiletype=="txt")
// alert(myajax.responseText)

return myajax.responseText
}
}
}
function deviceLookup(hostname){
if(hostname.length > 0){
var retVal = ajaxpack.postAjaxRequest("lookupHost.php", "hostname="+hostname, processGetPost, "txt");
if(retVal == '1'){
return true;
}else{
return false;
}
}else{
return false;
}
}

function validate(frm){
if(frm.DeviceName.value == ''){
alert("Server Name is required");
frm.DeviceName.focus();
return false;
}else{
if(!deviceLookup(frm.DeviceName.value)){
alert("Host name not found");
frm.DeviceName.focus();
return false;
}
}
}

If I alert alert(myajax.responseText), I see a 1 or 0 returned from the processing script. The problem is that retVal in deviceLookup() is null or empty. I think the ajax call is running independently, and not procedurally (I'm guessing because of the call back). Any ideas on how to easily pull this off?

Thanks!
hanji