转换ajax转发

时间:2013-02-26 11:10:15

标签: php jquery ajax

我通过AJAX插入数据,并使用GET请求发送数据。现在我想使用POST请求发送数据。我应该如何转换此代码?

<form name=myf id=myf>
    <input type=text name=user>
    <input type=text name=email>
    <input type=button  onclick="ready();" value="Add">
</form>


<script languague=javascript>
    function ready(){           
        var url="adduser.php";
        var parm="?user="+document.myf.user.value;
        parm+="&email="+document.myf.email.value;
        //alert(parm);
        var target=document.getElementById('abc');
        add(url,parm,target);
        document.getElementById('myf').reset();
    }
</script>

2 个答案:

答案 0 :(得分:1)

使用jQuery:

$.post(url, {'user':user, 'someotherdata':'data'}, function (response) {
    //do the result oriented activities
});

答案 1 :(得分:0)

var xmlhttp;

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        //Code after ajax response
    }
}

xmlhttp.open("POST","adduser.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("user="+document.myf.user.value+"&email="+document.myf.email.value");