为什么我在ajax Retrieving函数后无法自动运行JS函数?

时间:2012-01-25 01:27:07

标签: php javascript jquery ajax

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      $("#button1").click(function(){
 if(document.getElementById("hidValid").value == "Correct Username & Password"){
             parent.result.location.href="user.php";
            window.location.href="welcomeuser.php";
         }
      });
    });
    </script>
    <script type="text/javascript">
    function validate_use_pass(username,password){
        var loginUserID = document.getElementById("loginUserID").value;

        var passUserID = document.getElementById("passUserID").value;

        if(loginUserID == ""){
            alert("Invalid Username/Password.");
        }else if(passUserID == ""){
            alert("Invalid Username/Password.");
        }
        if(loginUserID == "admin" && passUserID =="admin" ){
    parent.result.location.href="account.php";
    window.location.href="welcome.php";
        }else{
            var xmlhttp;
            /*if(username.length==0 || password.length==0){
                document.getElementById("validateText").value="Enter Username/Password";
        return;
    }*/
    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("validateText").innerHTML=xmlhttp.responseText;
        document.getElementById("hidValid").value=xmlhttp.responseText;
        }
      }

    xmlhttp.open("GET","validateUsePass.php?username="+username+"&password="+password ,true);
    xmlhttp.send();
}

}

问题是我似乎无法在“xmlhttp.send();”之后运行一个函数。 因为当我登录时,我需要按下2x按钮来运行JS功能。

首先按下按钮将从PHP中检索数据然后第二次按下将运行JS功能。

1 个答案:

答案 0 :(得分:3)

试试这个:

    $.ajax({
        type : 'GET',
        url : 'validateUsePass.php',
        data: {
            username:$('loginUserID').val(), 
            password:$('passUserID').val()
        },
        success : function(data){
            alert('success')
        },
        error  : function(data){
            alert('error')          
        }

    } );