Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save akhileshdarjee/c059a4d834a4d8cb80d2927eba4abc44 to your computer and use it in GitHub Desktop.
Save akhileshdarjee/c059a4d834a4d8cb80d2927eba4abc44 to your computer and use it in GitHub Desktop.
send files, images or documents via AJAX
<!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