0

I have a form using a file upload field with a multiple tag. Now I would like to pass it to php via FormData(). This Is what I have tried:

<input class="requiredField" type="file" accept=".xls,.xlsx,.csv,.pdf,.msg" name="commitment_publication_communication[]" multiple required>

jQuery:

var commitment_publication_communication = tool_form_annex.find('input[name="commitment_publication_communication[]"]')[0].files;   
var fd = new FormData();
$.each(commitment_publication_communication, function (i, file) {
   fd.append('c_publication_communication[]', file);
});

PhP:

$c_publication_communication = $_FILES['c_publication_communication'];
for ($i = 0; $i < count($c_publication_communication); $i++) {
    media_handle_upload('c_publication_communication', 0);
}

EDIT: Full Ajax request

$.ajax({
   url: ajaxurl,
   type: 'POST',
   processData: false,
   contentType: false,
   data: fd,
   success: function (response) {
        response = JSON.parse(response);
        clickedButton.next().css('display', 'none');
        clickedButtonNext.next().css('display', 'block');
        clickedButtonNext.next().css('color', '#8ed600');
        clickedButtonNext.next().css('margin-top', '20px');
        clickedButtonNext.next().text(devplus_data.successfully_saved_temporarily_message);
        
   },
   error: function () {
        clickedButton.next().css('display', 'none');
        clickedButtonNext.next().css('display', 'block');
        clickedButtonNext.next().css('color', 'red');
        clickedButtonNext.next().css('margin-top', '20px');
        clickedButtonNext.next().text(devplus_data.failed_to_temporarily_save_message);
  }
});

I feel like I am going the right way but it's not working.. Any help would be appreciated!

  • the answer already is given here https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array – Mohsin Marui Sep 18 '20 at 07:14
  • You forgot to tell us => what is not working ? – Always Helping Sep 18 '20 at 07:20
  • @AlwaysHelping the media_handle_upload function. Also, when I try to return $c_publication_communication, it returns null. So the inputted files did not go through I guess. – Agoria_TeamDigital Sep 18 '20 at 07:32
  • Can you add your ajax jQuery code where you are sending the formData ? the acutual ajax request ? – Always Helping Sep 18 '20 at 07:32
  • try like this => `$c_publication_communication = $_FILES['c_publication_communication'];` you do not need to `[]` in arguments – Always Helping Sep 18 '20 at 08:29
  • only in PHP use this `$c_publication_communication = $_FILES['c_publication_communication']; var_dump($c_publication_communication)` – Always Helping Sep 18 '20 at 10:53
  • in your jQuery code => `var getFiles = $('input[name="commitment_publication_communication[]"]')[0].files $.each(getFiles, function (i, file) { fd.append('c_publication_communication[]', file); });` – Always Helping Sep 18 '20 at 10:55
  • @AlwaysHelping I've made some edits to my question. The jquery now returns an array so that's OK. Now my php loop to handle the upload still doesn't work. – Agoria_TeamDigital Sep 18 '20 at 14:37
  • try the PHP i have provided you - that should show some `var_dump` with array of files from the request - ajax is sending the files. – Always Helping Sep 18 '20 at 14:50
  • @AlwaysHelping when I add var_dump I get the error 'Uncaught SyntaxError: Unexpected token a in JSON at position 0' in my console. – Agoria_TeamDigital Sep 21 '20 at 07:53

0 Answers0