Skip to content

Instantly share code, notes, and snippets.

@jokosusilo
Created April 26, 2017 04:21
Show Gist options
  • Save jokosusilo/85357f6f0ea97378d94667e0c9027da9 to your computer and use it in GitHub Desktop.
Save jokosusilo/85357f6f0ea97378d94667e0c9027da9 to your computer and use it in GitHub Desktop.
Upload image via AJAX
<?php
// Do what you want
print_r($_POST);
// Need jquery before this script
jQuery('#form').on('submit', function(e){
var formData = new FormData();
formData.append("file", $('#form #file')[0].files[0]);
formData.append('caption', jQuery('#form #caption').val());
$.ajax({
url: '/path/to/file',
type: 'POST',
cache: false,
contentType: false,
processData: false,
data: formData,
})
.success(function() {
console.log("success");
});
});
<form method="POST" id="form">
<input type="file" name="file">
<input type="text" name="caption">
<input type="submit" name="submit" value="Submit">
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment