0

I have two different file inputs and multiple textual/select inputs, that I'd like to upload to a PHP file using Ajax. The file inputs are meant to send images, and because they are two specific images that I'd like to identify by the name of the input, I do not want to use <input type="file" multiple>.

Currently I have the following (names have been changed to keep it simple):

<input type="file" name="file1">
<input type="file" name="file2">
<textarea name="text1"></textarea>
<button>Submit</button>

What I have tried is to push both to a variable once the change event of the file input gets fired, followed by the button press triggering the upload using Ajax.

$('input[type="file"][name="file1"]').on('change', prepareUpload);
$('input[type="file"][name="file2"]').on('change', prepareUpload);

files = new Array;

function prepareUpload(event){
    files.push(event.target.files);
}

$('button').on('click', uploadFiles);

function uploadFiles(event){

    $('#uploadInfo').html('Uploading...');
    event.stopPropagation();
    event.preventDefault();

    var data = new FormData();
    $.each(files, function(key, value){
        data.append(key, value);
    });

    data.append('text1', $('textarea[name="text1"]').val());

    $.ajax({
        url: '',
        type: 'POST',
        data: data,
        cache: false,
        dataType: 'json',
        processData: false,
        contentType: false, 
        success: function(result){
            //do stuff
        },
        error: function(error){
            console.log(error);
        }
    });
}

But this seems to do nothing. Is it even possible to append two image inputs to the same request using Ajax, or am I better off trying to put it in two different requests?

6
  • Hi and Welcome to Stack Overflow. This is not a solution to your problem but a comment: You have two files with the same name in your HTML. This will not work. I believe it is just a typo, but it may be in your code also. Commented Nov 8, 2018 at 13:18
  • Ah thanks, it's not in my HTML, as I changed all names to make it a bit more simple for other possible use cases and others maybe having the same question :) Commented Nov 8, 2018 at 13:21
  • any console errors? Commented Nov 8, 2018 at 13:27
  • None, ajax only fires the error function as it isn't getting json back from my PHP file. But the PHP file is getting an empty request. Commented Nov 8, 2018 at 14:16
  • how do you know its getting an empty request? Commented Nov 8, 2018 at 14:20

1 Answer 1

1

event.target.files is a array, so you need the first file in the array

function prepareUpload(event){
    files.push(event.target.files[0]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this made it a little more clear for me. Do the new files get prepended or appended to the array? (is it fair to continuously use event.target.files[0] for every file or do I need to increment the 0?)
you have multiple inputs with one file each so you will always have event.target.files[0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.