Javascript AJAX function not working properly

Posted by Or W on Stack Overflow See other posts from Stack Overflow or by Or W
Published on 2011-01-17T22:45:21Z Indexed on 2011/01/17 23:53 UTC
Read the original article Hit count: 266

I have a function that sends a GET request to a php script and checks if the script returned any output. It works great, but when I try to add another function that checks for something similar, both of them fail. What am I missing?

    function checkUsername(usr,n) {
        var user = usr.val(), xmlhttp;

        //var str = document.getElementById('email').value;
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            //document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
                if (xmlhttp.responseText != "") {
                    usr.addClass( "ui-state-error" );
                    updateTips( n );
                    return false;
                }
                else {
                    return true;
                }
            }
          }
        xmlhttp.open("GET","ajaxValidate.php?type=user&q="+user,true);
        xmlhttp.send();

    }

The above works perfectly, when adding this function, none of them work:

    function checkEmail(em,n) {
        var email = em.val(), xmlhttp;

        //var str = document.getElementById('email').value;
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            //document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
                if (xmlhttp.responseText != "") {
                    em.addClass( "ui-state-error" );
                    updateTips( n );
                    return false;
                }
                else {
                    return true;
                }
            }
          }
        xmlhttp.open("GET","ajaxValidate.php?type=email&q="+email,true);
        xmlhttp.send();
    }

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about AJAX