Last active
January 2, 2021 14:43
-
-
Save akhileshdarjee/c059a4d834a4d8cb80d2927eba4abc44 to your computer and use it in GitHub Desktop.
send files, images or documents via AJAX
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>User Form</title> | |
</head> | |
<body> | |
<input type="hidden" name="user_id" value="1"> | |
<form name="user-form" id="user-form" enctype="multipart/form-data"> | |
<input type="file" name="image_file"> | |
<input type="text" name="name"> | |
<button type="button" title="Save" id="save">Save</button> | |
</form> | |
<script type="text/javascript"> | |
$("#save").on("click", function() { | |
var user_form = $(this).closest("#user-form"); | |
var details = new FormData($(user_form)[0]); | |
// you can also append any custom paramter to form data, if not skip these | |
// details.append('user_id', $('body').find('[name="user_id"]').val()); | |
$.ajax({ | |
type: 'POST', | |
url: '/save-user', | |
data: details, | |
processData: false, | |
contentType: false, | |
success: function(data) { | |
console.log("user updated successfully"); | |
}, | |
error: function(e) { | |
console.log("error"); | |
} | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment