开发者

Javascript AJAX function not working properly

开发者 https://www.devze.com 2023-02-05 00:23 出处:网络
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

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" );
         开发者_JAVA技巧           updateTips( n );
                    return false;
                }
                else {
                    return true;
                }
            }
          }
        xmlhttp.open("GET","ajaxValidate.php?type=email&q="+email,true);
        xmlhttp.send();
    }


Try following:

   function createRequestObject() {
        var xmlhttp=null;
        if (window.XMLHttpRequest) {
          xmlhttp=new XMLHttpRequest();
        } else {
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        return xmlhttp;
    }

function checkUsername(usr,n) {
    var user = usr.val();
    var xmlhttp = createRequestObject();
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState==4 && xmlhttp.status==200) {
         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();
}

function checkEmail(em,n) {
    var email = em.val();
    var xmlhttp = createRequestObject();
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState==4 && xmlhttp.status==200) {
          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();
}


In both functions, let the first line be:

var xmlhttp;
0

精彩评论

暂无评论...
验证码 换一张
取 消