Created
July 24, 2020 14:56
-
-
Save sashabeep/420027696f81d0dce4a702a09071f91f to your computer and use it in GitHub Desktop.
Send form via ajax with attachments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).on('submit', 'form.myFormWithFiles', function (event) { | |
event.preventDefault(); | |
event.stopImmediatePropagation(); | |
var $this = $(this); | |
var formData = new FormData($($this)[0]); | |
$.ajax({ | |
type: $this.attr('method'), | |
url: $this.attr('action'), | |
data: formData, | |
processData: false, | |
contentType: false, | |
dataType: "html", | |
success: function (response) { | |
//any success behavior - update popup, etc | |
$this.get(0).reset(); | |
var response = response; | |
$("#response").remove(); | |
$('body').append('<div id="response" style="display:none">'); | |
$("#response").html(response); | |
$("#response").show(); | |
} | |
}); | |
return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment