Reputation: 59
I am seeing two methods being used to submit form via ajax. Which one is valid? If the first one is, how come it's not making use of $ajax?
Method 1:
$(form).on("submit", function (event) {
event.preventDefault();
$(this).serialize();
});
Method 2:
$(function() {
$(form).on("submit", function (event) {
$.ajax({
type: "POST",
url: "pathscript.php",
data: $(this).serialize()
}).done(function() {
}).fail(function() {
});
event.preventDefault(); // Prevent the form from submitting via the browser.
});
})
Upvotes: 0
Views: 49
Reputation: 1593
Method 1 doesn't send any request and form won't be submitted. Method 2 is the way you should do it.
$(this).serialize();
This will just serialize your from content.
So as the final conclusion Method to is the correct way and only it is doing the AJAX form submit.
Upvotes: 2