Separate data from multiple forms with Formdata

Asked

Viewed 108 times

0

It would have been possible to send some formularies at the same time with Formdata?

For example, each product will have sizes and images. It would have to separate each product into an array and send it at the same time to the PHP page, so I could walk through them with a foreach and register.

I tried that, 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);
        }
    });

inserir a descrição da imagem aqui

  • Sorting doesn’t matter, since your PHP processing will reorganize, as the name of each POST, the array.

  • The only important factor is to know if the data is being sent correctly, according to each name, in the output.

  • Has a photo/print of request, with the information generated?

  • I put the print on the question. Do you know where you have the "GG" and "M"? GG belongs to one product and M belongs to another. I have to create a "colors" index for each product.

  • Note: The "title" will be the same for all products, so do not need to stay within the "colors", this part is correct.

  • The problem is that it is creating only one "color" and putting all the data inside this, instead of separating.

  • The problem is in your logic. Once you create a name request, it is unique. You should do something like: cores_images and cores_sizes to be able to specify better.

  • Adding images and sizes is correct, the problem is to separate products. It had to generate something in this style: colors ['images' => ['imagem1', 'imagem2'], 'sizes' => [’m', 'G']], colors ['images' => ['imagem3', 'imagem4'], 'sizes' => ['P', 'GG']]

  • So knife: 'colors[images][ '+index+' ]'

  • 1

    Man, that’s right. Thanks for the help!

  • I will post the reply, mark as completed.

Show 6 more comments

1 answer

1


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

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

....

  • 1

    I only had to make a small change: 'colors[' + index + '][sizes][]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.