使用PHP调用Ajax

时间:2015-11-04 23:57:22

标签: php ajax

我正在尝试使用AJAX和PHP构建一个简单的注册表单。在提交表单时,将调用submit_create_user_form()函数。

这个AJAX函数调用ajax_calls.php页面。

function submit_create_user_form(){
    var u = document.getElementById("org_name").value;

        xhttp.onreadystatechange = function(){
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                var result =xhttp.responseText; 
                //status.innerHTML=xhttp.responseText;
                if(result==="Successful"){
                    //window.scrollTo(0,0);
                    document.getElementById('main').innerHTML ="An activation link has been sent to the new user's E-mail ID";
                }
                else
                    status.innerHTML="Some error Occurred!!";
            }       
          }
          xhttp.open("POST", "ajax_calls.php", true);
          xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

          xhttp.send("q="+u);


}

ajax_calls.php回应“成功”,在上面的代码片段中用xhttp.responseText检查。 即使这样,输出也是“发生了一些错误。”

ajax_calls.php:

if( isset($_POST["q"]) ){
    echo "Successful";
}

有人可以帮忙吗?谢谢!

2 个答案:

答案 0 :(得分:-1)

这是jQuery代码

$(document).ready(function(e){
        var u=$('#org_name').val();
        $.post("ajax_calls.php",{q:u},function(data,message,xhr){
                                if(message==='success'){
                                    $('#main').text(data);
                                }
                            });

    });

答案 1 :(得分:-1)

代码中的问题是:

您没有声明xhttp添加此代码:

var xhttp = new XMLHttpRequest();