passing data in form submission via jquery/ajax

北城以北 提交于 2019-12-03 13:54:32
Oleg

The jQuery.serialize can be helpful for you. It is important that you use name property for all fields of the form which you want to submit. The corresponding code can be about the following

$("form#myFormId").submit(function() {
    var mydata = $("form#myFormId").serialize();
    console.log(mydata); // it's only for test
    $.ajax({
        type: "POST",
        url: "myUrlToPostData.php",
        data: mydata,
        success: function(response, textStatus, xhr) {
            console.log("success");
        },
        error: function(xhr, textStatus, errorThrown) {
            console.log("error");
        }
    });
    return false;
});
$.ajax({
type: "POST",
url: "handle.php",
data: "n="+name+"&e="+email,
success: function() {
alert('It worked');
}
});

Here is the simplest way to use it. This would just send two post variables, $_POST['n'] and $_POST['e'], to handle.php. Your form would look like this if your ajax is in a function called send():

<form id="form" onsubmit="send(this.name.value, this.email.value); return false;">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!