请在Ajax中等待消息

时间:2015-12-25 13:15:21

标签: php html ajax

我是ajaxjs的新手。我正在尝试将数据发送到functions.php?action=signp,但代码显示“请稍候”,然后没有任何反应。我的代码如下:

<script type="text/javascript">
function signup()
{
    var ufname = document.getElementById("fName").value;
    var ulname = document.getElementById("lName").value;
    var e = document.getElementById("email").value;
    var p1 = document.getElementById("pass1").value;
    var p2 = document.getElementById("pass2").value;
    var status = document.getElementById("statusSignUp");
    if(ufname == "" || ulname == "" || e == "" || p1 == "" || p2 == "")
    {
        status.innerHTML = "Fill out all of the form data";
    }
    else
    {
        document.getElementById("signupbtn").style.display = "none";
        status.innerHTML = 'please wait ...';
        var ajax = ajaxObj("POST", "functions.php?action=signp");
        ajax.onreadystatechange = function()
        {
            if(ajaxReturn(ajax) == true)
            {
                if(ajax.responseText != "signup_success")
                {
                    status.innerHTML = ajax.responseText;
                    document.getElementById("signupbtn").style.display = "block";
                } 
                else
                {
                    window.scrollTo(0,0);
                    document.getElementById("signupform").innerHTML = "Yippieeee";
                }
            }
        }
        //xhttp.open("POST", "functions.php?action=signp", true);
        ajax.send("&fName="+fName+"&lName="+lName+"&email="+email+" pass1="+pass1);
    }
}
</script>

有人请告诉我代码中有什么问题吗?

2 个答案:

答案 0 :(得分:0)

我会为你提供一个非常简单的解决方案。使用jQuery Ajax。它是一个非常容易使用的库。

http://api.jquery.com/jquery.ajax/

答案 1 :(得分:-1)

这里我如何使用我的网站,希望我能给你一些关于jquery ajax的见解: (为了清楚起见,所有网址和变量都已简化

if(error_flag== 0)//i checked for errors before execute ajax.this is error flag that counts error if any error occurs it will shot its message.
    {
        e.preventDefault();
        $('#registerModal').modal('show');//i show modal to user.In that modal i have spining icon and text that tells them to wait
        //ajax post to register.php
        $.post("register.php",
        $("#registerForm").serialize(),
        function(durum){
            durum = jQuery.parseJSON(durum);
            $("#registerMesaj_id").html(durum.Msj);//if all went succesfull it will say success message.ıf not it will tell user some error
        });
    }else{
        e.preventDefault();
        console.log(error_message);
    }