passing data in form submission via jquery/ajax

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 21:29:46

问题


i want to submit a form with about 10 input using jquery/ajax,but i don't know how can i pass the data to it through data parameters of ajax.should i serialize them ?


回答1:


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;
});



回答2:


$.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;">


来源:https://stackoverflow.com/questions/5929790/passing-data-in-form-submission-via-jquery-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!