Created
April 26, 2017 04:21
-
-
Save jokosusilo/85357f6f0ea97378d94667e0c9027da9 to your computer and use it in GitHub Desktop.
Upload image 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
<?php | |
// Do what you want | |
print_r($_POST); |
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
// 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"); | |
}); | |
}); |
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 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