file upload works with ajax and php, but does not return responses! in the upload.php page is displayed:
uploaded succesfully!
html:
<form action="upload.php" method="post" enctype="multipart/form-data" id="form">
<div style="padding: 15px;">
<input type="file" name="file" id="file">
</div>
<div style="padding: 15px">
<input type="submit" value="Upload" id="upload">
</div>
</form>
ajax:
$(document).ready(function () {
$('#form').on('submit', function (e) {
e.preventDefault();
$.ajax({
url: "upload.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function (response) {
alert(response);
}
})
})
})
php:
if (isset($_FILES['file'])) {
$file_destination = "uploads/IMG-" . date("Y-m-d") . time() . ".jpg";
if (move_uploaded_file($_FILES['file']['tmp_name'], $file_destination)) {
echo "uploaded succesfully!";
}
}