Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing data in form submission via jquery/ajax

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 ?

like image 622
hd. Avatar asked May 08 '11 19:05

hd.


2 Answers

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;
});
like image 196
Oleg Avatar answered Sep 23 '22 01:09

Oleg


$.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;">
like image 44
Mike Soule Avatar answered Sep 24 '22 01:09

Mike Soule