Separate Data from Multiple Forms with FormData

0

Would you like to submit some forms at the same time with FormData ?

For example, each product will have sizes and images. I would like to separate each product in an array and send it to the PHP page at the same time, so I can go through them with a foreach and make the registration.

I tried this way, but it mixes the data of all products, instead of separating:

    var dados = new FormData();

    $('.copiar-elemento').each(function() {
        $(this).find("select[name*='tamanho']").each(function(index, element) {
            dados.append('cores[tamanhos][]', element.value);
        });

        $(this).find('input:file').each(function(index, element) {
            dados.append('cores[imagens][]', element.files[0]);
        });
    });

    $.ajax({
        url: $('#form-produto').attr('action'),
        type: 'POST',
        data: dados,
        dataType: 'json',
        processData: false,
        contentType: false,
        cache: false,
        success: function(data) {
            console.log(data);
        }
    });

    
asked by anonymous 09.04.2018 / 15:05

1 answer

1

According to the comment, the assembly of append should be:

 'cores[' + index + '][imagens][]
 'cores[' + index + '][tamanhos][]

....

    
09.04.2018 / 16:46