tags:

views:

341

answers:

2

Below is my code which I am trying to find 'Username Ok' in the respone text, and then flag UserNameOk as true. I cannot get this working.

function check_username(username)
{
    var httpRequest;
    make_request()
    function stateck() 
    {

     if(httpxml.readyState==4)

      { 
      if (httpxml.responseText == "Username Ok")
      {
        UserNameOk = true;

      }
       else 
      {
         UserNameOk = false;
      }
     checkCanSubmit();
     document.getElementById("user_div").innerHTML=httpxml.responseText;
      }
    }

    httpxml.onreadystatechange=stateck;
    user_url="ajax_username.php?username=" + username.value;
    httpxml.open("GET",user_url,true);
    httpxml.send(null);

}

Below is my checkCanSubmit code

function checkCanSubmit()
{
    if (UserNameOk && PasswordOk && EmailOk)
    {
    document.getElementById("button").disabled= false;
    }
    else
    {
    document.getElementById("button").disabled= true;
    }
    }

Any help will be appreciated Thanks.

A: 

Are you seeing the correct response come in through firebug?

First step is to make sure your server is returning the correct response by looking at firebug. I might also simplify the code to help narrow down the issues. Try:

function check_username(username)
{
    var httpRequest;
    make_request()
    function stateck() 
    {

            if(httpxml.readyState==4)

             {  
                    if (httpxml.responseText == "Username Ok")
                    {
                             alert('1');
                             UserNameOk = true;

                    }
                     else 
                    {
                            alert('2');
                            UserNameOk = false;
                    }
             }
    }

    httpxml.onreadystatechange=stateck;
    user_url="ajax_username.php?username=" + username.value;
    httpxml.open("GET",user_url,true);
    httpxml.send(null);

Can you be more specific about where the problem is?

Tony
Yeh I no the correct response is coming through because I see the response from this line: document.getElementById("user_div").innerHTML=httpxml.responseText;That works fine. My problem is that the responseText is showing as "Username Ok" but it is not being flagged as true.
Elliott
I have just tested the above and I get the alert two. Although I have checked the responseText and it does equal Username Ok
Elliott
yea maybe you need to strip the response. actually throw alert(httpxml.responseText); right before the if statement and see what it prints
Tony
Just done that, I am still only getting Username Ok
Elliott
ah nevermind, elliott just did that. Use this function to truncate the response to 11 characters:function truncate(str, len){ if(str.length > len){ return str.substring(0, len).replace(/\s+$/,'') + '...'; }else{ return str; }}
Tony
Thanks, How am I using that function? like this?str = httpxml.responseText; len = httpxml.responseText.length; truncate(str, len);
Elliott
+1  A: 

Just a guess: the response might be something like "Username Ok\n" (the '\n' being a newline character which you can't see). Or it might have whitespace at the beginning or end. You could print out (alert) the length of the string to test that.

joeytwiddle
I have just checked the lenght (httpxml.responseText.length) and it says its 40? I have also just alerted the response and checked its "Username Ok" which it is
Elliott
Well 40 is way too long, there must be some characters you aren't seeing. A quick fix for you might be:`if (httpxml.responseText.indexOf("Username Ok") >= 0)`But if you really want to find out what these invisible characters are, you could try: `alert(escape(httpxml.responseText));`I wonder if it will be something like "<HTML><BODY>Username Ok</BODY></HTML>" (37 chars). alert() probably hides the tags. ;)
joeytwiddle
Thanks I used (httpxml.responseText.indexOf("Username Ok") >= 0)Seems to be working :)
Elliott