0

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){
                    }
         });
    });
Sylarman5
  • 110
  • 1
  • 6
Ed91gg
  • 307
  • 3
  • 13
  • Have you tried checking what the ajax is posting to ensure everything is there? – Matt Jan 11 '18 at 23:03
  • `$column1', '$column2'` what are those supposed to represent? you have 2 text inputs and 1 file input. The question's unclear in more ways than shown. – Funk Forty Niner Jan 11 '18 at 23:04
  • May have to pass as FormData... more reading: https://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously and https://stackoverflow.com/questions/37825913/how-to-pass-file-data-with-ajax-and-jquery – IncredibleHat Jan 11 '18 at 23:05
  • @Matt I think everything is there, since if I remove the ajax to switch it to php submit it does work everything. Is so weird, I've been 2 days breaking my head on this. – Ed91gg Jan 11 '18 at 23:06
  • @Ed91gg but have you actually checked using `var_dump($_POST)`? Just because it works one way doesn't means it's definitely being sent – Matt Jan 11 '18 at 23:07
  • .serialize() can't handle files. – Kevin B Jan 11 '18 at 23:07
  • @FunkFortyNiner yes, those are html inputs, didn't post the html, thought it could be clear to understand with the information above? – Ed91gg Jan 11 '18 at 23:07
  • @KevinB Didn't know that, can you post any link or comment the right way to handle images? Thanks for that piece of info, didn't know that. – Ed91gg Jan 11 '18 at 23:09
  • Yup, i marked it as a dupe. you can use FormData to send both the files and the other inputs. – Kevin B Jan 11 '18 at 23:09
  • 1
    Didnt read the two links I posted? They explain using FormData to pass files with form data through ajax. ***sigh*** – IncredibleHat Jan 11 '18 at 23:09
  • @IncredibleHat Yes, I'm actually on that and getting it pretty good, thanks! – Ed91gg Jan 11 '18 at 23:13
  • @KevinB Sorry for that, thanks, might delete this question then! Good day! – Ed91gg Jan 11 '18 at 23:13

0 Answers0