So, everything works fine if I insert my form info with pure PHP, everything inserts into my DB including the –input "type/file"– (meaning, the image too).
But when it comes to insert it via AJAX, it inserts everything BUT the image. For some reason the image is the only thing I can't get to insert when it comes to doing it via AJAX. Here's my code.
AJAX CALL
$('#insert_form').click(function(e){
$.ajax({
type: 'POST',
url: 'insert_form_backend.php',
data: $('#__form__').serialize(),
success: function() {
alert("Insert Successful");
}
});
e.preventDefault();
});
insert_form_backend.php
<?php
require_once "mysql/db.php";
$name = $_POST['name'];
$surname = $_POST['surname'];
$photo = $_FILES['photo']['name'];
$photo_temp = $_FILES['photo']['tmp_name'];
$photo_path = "folder_temp_images/$photo";
move_uploaded_file($photo_temp, $photo_path);
$query = "INSERT INTO my_table(column1, column2) ";
$query .= "VALUES ('$column1', '$column2')";
$result = mysqli_query($connection, $query);
?>
As I said above, it ONLY inserts everything (including image) if I submit the form with pure PHP, meaning, it refreshes the whole page.
Thanks, I don't want to throw in the towel on this, I really want to make this with ajax. I hope we could fix this!
FOR FILE IN AJAX try this method:
$("#photo").change(function(){
var file_datta = $('#photo').prop('files')[0];
var form_datta = new FormData();
form_datta.append('file', file_datta);
$.ajax({
url: 'insert_form_backend.php', // point to server-side PHP script
dataType: 'text', // what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
data: form_datta,
type: 'post',
success: function(php_script_response){
}
});
});