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 ?
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;">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With