Last active
May 14, 2018 21:52
-
-
Save probil/0ac5160febbdfc429e5e to your computer and use it in GitHub Desktop.
Usefull function to serialize form inputs including input[type=file]
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
//USAGE: $("#form").serializefiles(); | |
(function($) { | |
$.fn.serializefiles = function() { | |
var obj = $(this); | |
/* ADD FILE TO PARAM AJAX */ | |
var formData = new FormData(); | |
$.each($(obj).find("input[type='file']"), function(i, tag) { | |
$.each($(tag)[0].files, function(i, file) { | |
formData.append(tag.name, file); | |
}); | |
}); | |
var params = $(obj).serializeArray(); | |
$.each(params, function (i, val) { | |
formData.append(val.name, val.value); | |
}); | |
return formData; | |
}; | |
})(jQuery); |
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
$('form.edit-account-form').submit(function(e){ | |
//stop std action | |
e.stopPropagation(); | |
e.preventDefault(); | |
//send data | |
$.ajax({ | |
type : 'POST', | |
url : $(this).attr('action'), | |
data : $(this).serializefiles(), | |
processData: false, | |
contentType: false, | |
success : function(data){ | |
console.log(data); | |
} | |
}); | |
return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment