Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AJAX result returns too fast

Status
Not open for further replies.

AndyInNC

Programmer
Sep 22, 2008
76
US
I'm doing a simple database validation. To avoid duplicate names, I want to see if the First and Last names are in the database and alert the user before writing the data. So I've got an ASP page that works fine. It returns the count of records where FName like 'xxx%' and LName like 'yyy%'. Running it alone it works great, so no problems there.

Calling it is a JS/Ajax call here:
Code:
    var sUrl = "Includes/CommonLogic_ExistingName.asp";
    sUrl = sUrl + "?sFirst=" + sFirst;
    sUrl = sUrl + "?sLast=" + sLast;
    sUrl = sUrl + "&sid=" + Math.random();

    oXmlHttp.onreadystatechange = function() {
        if (oXmlHttp.readyState == 4)
            {
                if (oXmlHttp.status == 200)
                    {
                        var sTemp = oXmlHttp.responseText;
                        
                        if (sTemp.length > 0)
                            {
                            if (sTemp > 0)
                                {
                                return true;
                                }
                            else
                                {
                                return false;
                                }
                            }
                    }
            }
    }
    oXmlHttp.open("GET", sUrl, false);
    oXmlHttp.send(null);
When I call it, the return value is always "undefined" (if I store it/view it in a var). With the following, I almost always get "not found", whether the name exists or not.:
Code:
function func_TestNewName()
{
    if (func_IsExistingName(txtFName.value, txtLName.value) == true)
        {
            alert("This name already exists.");
        } else {
            alert("This name was not found");
        }
}

I've changed the Open to synch which helped once, but it's not consistent. Am I missing something or is there a better way to make it wait to return until XmlHttp.status is 200?

Thanks.
 
Please note this is an asp page, and you're having AJAX and JS issues, should be taken up in appropriate forums.


the only thing coming to mind is to verify a sample string assembled from your script in a seperate window to ensure that the external pull is operating correctly, my assumption is either the external page is malfunctioning, or our JS isn't assembling the querystring correctly and misfiring the external.


[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
" I always think outside the 'box', because I'm never in the 'loop' " - DreX 2005
 
You're right, let me repost in a better location.

And thanks for the help.
 
var sUrl = "Includes/CommonLogic_ExistingName.asp";
sUrl = sUrl + "?sFirst=" + sFirst;
sUrl = sUrl + "?sLast=" + sLast;
sUrl = sUrl + "&sid=" + Math.random();

Just as a start, with the above code you provided, "sLast" should be preceeded with an ampersand, not a question mark...

Code:
    var sUrl = "Includes/CommonLogic_ExistingName.asp";
    sUrl = sUrl + "?sFirst=" + sFirst;
    sUrl = sUrl + "[b][COLOR=red]?[/color][/b]sLast=" + sLast;
    sUrl = sUrl + "&sid=" + Math.random();


TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
ouch![surprise] [blush]

Didn't fix the problem at hand, but thanks for the proofreading
 
should the bolded part below be sTemp.length?

Code:
oXmlHttp.onreadystatechange = function() {
        if (oXmlHttp.readyState == 4)
            {
                if (oXmlHttp.status == 200)
                    {
                        var sTemp = oXmlHttp.responseText;
                        
                        if (sTemp.length > 0)
                            {
                            [b]if (sTemp > 0)[/b]
                                {
                                return true;
                                }
                            else
                                {
                                return false;
                                }
                            }
                    }
            }
    }

If yes, you can get rid of one of your conditions as you're duplicating it...you're checking to see if it is greater than 0, and then checking it again.

Code:
	oXmlHttp.onreadystatechange = function() {
        if (oXmlHttp.readyState == 4)
            {
                if (oXmlHttp.status == 200)
                    {
                        var sTemp = oXmlHttp.responseText;
                        
                        if (sTemp.length > 0)
							return true;
						else
							return false;
                    }
            }
    }

which can be further cut down with an in-line return:

Code:
		oXmlHttp.onreadystatechange = function() {
			if (oXmlHttp.readyState == 4)
				{
					if (oXmlHttp.status == 200)
						{
							var sTemp = oXmlHttp.responseText;
							return (sTemp.length > 0) ? true : false;
						}
				}
		}

In the end, a lot less code, easier to read...

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
[1] If the response is simple, it is not completely unreasonable to use a synchronous setup. In that case, it is this.
[tt]
var sUrl = "Includes/CommonLogic_ExistingName.asp";
sUrl = sUrl + "?sFirst=" + sFirst;
sUrl = sUrl + "&sLast=" + sLast;
sUrl = sUrl + "&sid=" + Math.random();

//completely get rid of the setting of .onreadystatechange, because you are using synchronous setup

oXmlHttp.open("GET", sUrl, false);
oXmlHttp.send(null);
if (oXmlHttp.readyState!=4 || oXmlHttp.status!=200) {
return "message on something no good"
} else {
return oXmlHttp.responseText; //or any derived quantity from responseText
}
[/tt]
[2] If you use setting onreadystatechange to return something, that return is sent to the garbage collection. You have to, instead of return ..., assign the intended return to a global variable as a mean to communicate with the savePerson() function. And the savePerson() function must lookup the global variable. If it is not set as intended, use a setTimeout to call itself again. But in the successive call to itself, it must not use the checkIfNameExists() again. That latter can be controlled by again a global variable to determine it, etc etc. I would be very verbose to describe the mechanism by words. In any case, the return from the onreadystatechange handler will not be captured by the myTemp as such - and that is the reason it is "undefined" always.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top