У меня есть форма
<form id="sendstories" enctype="multipart/form-data" >
<div class="add_picture col-xs-12 col-sm-12">
<div class="add_picture_block">
<div id="PicturePreview"></div>
<input id="uploadPicture" type="file" name="image" class="imgpicture" />
</div>
<div class="right_block_down_stories col-xs-12 col-sm-12">
<input type="submit" class="button_send_stories" value="Next" name="upload">
</div>
</div>
</form>
Скрипт:
<script>
$(document).ready(function() {
$('#sendstories').submit( function( e ) {
var formData = new FormData($(this)[0]);
$("#loading").css("display", "block");
$.ajax({
url: "include/addstory.php",
type: 'POST',
data: formData,
async: true,
cache: false,
contentType: false,
processData: false,
success: function (data) {
$("#loading").css("display", "none");
$('.js-overlay-thank-you').fadeIn();
$(this).find('input').val('');
$('#sendstories').trigger('reset');
},
});
return false;
});
});
</script>
И сам php файл:
<?php
$db = mysqli_connect("data", "data", "data", "data");
$msg = "";
if (isset($_POST['upload'])) {
$image = $_FILES['image']['name'];
$email = $_POST['email'];
$target = "../img/".basename($image);
$sql = "INSERT INTO TestPosts (image, email) VALUES ('$image', '$email')";
mysqli_query($db, $sql);
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Image uploaded successfully";
}else{
$msg = "Failed to upload image";
}
}
?>
С помощью AJAX данные не передаются
Хотя если передавать данные так:
<form method="POST" action="include/addstory.php" enctype="multipart/form-data">
все прекрасно работает