0

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());
    $.ajax({
        url: 'upload.php',
        dataType: 'html',
        data: form_data,
        processData: false,
        contentType: false,
        type: 'post',
        success: function(data) {
            console.log(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.

1
  • form_data.append('checkbox1', $('input:checkbox:checked').val()) Commented Jul 7, 2016 at 11:12

1 Answer 1

1

How about

var ch_data = [];

$('input[type="checkbox"]:checked').each(function(){
  ch_data.push($(this).attr('id'));
});

form_data.append('checkboxes', ch_data);
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, didn't expect it to be that simple. Thank you, works like a charm!

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.