0x1234 0x1234 - 7 months ago 28
Ajax Question

Post form with fileinput, textinput and multiple checkboxes to php via jquery ajax using formdata

I need to create a form where you can pick a file to upload, enter some text in an input field and check one or more categories represented by checkboxes. That form is posted to my php script with jquery ajax.

So far I've got the part for the file upload and input field working

$('#submit').click(function() {
var file_data = $('#fileToUpload').prop('files')[0];
var form_data = new FormData();
form_data.append('fileToUpload', file_data);
form_data.append('comment', $('#comment').val());
url: 'upload.php',
dataType: 'html',
data: form_data,
processData: false,
contentType: false,
type: 'post',
success: function(data) {


//upload file
echo "Filename" . $filename . " Comment:" . $_POST["comment"];

How can I append the data of the checkboxes and use them in my php script? Ideally it would just append the ids or data attributes of the checkboxes that were checked when submitting the form.


How about

var ch_data = [];


form_data.append('checkboxes', ch_data);