Sending form and array out of form via post

2

I have the following problem: I need to send via post the data of a form together with the data of an html table to which I add array in javascript. Unfortunately, I have no idea how to proceed in this case.

I ask colleagues to help with this challenge.

    
asked by anonymous 24.06.2015 / 02:47

2 answers

1

You have two options,

  • send everything via AJAX

  • send table data in hidden input

If you send by ajax you can use .serialize() of jQuery and send in ajax something like this:

var fData = $('form').serialize();
$.ajax({
   type: "POST",
   data: {form: fData, table: JSON.stringify(tDada)}, // onde tData é a array da tua tabela
   url: "index.php",
   success: function(msg)
   {
     $('.resposta').html(msg);
   }
});

If you want to put this table data inside a hidden field:

<input type="hidden" name="tabela" id="dadosTabela" />

and then inject the data in the table:

$('dadosTabela').val(JSON.stringify(fData));
    
24.06.2015 / 09:53
1

Eric , we have two options:

  • Serialize fields sent from fields using prefixed field name:

  • Manually construct the array with the fields submitted using the field name as a prefix:

    - "SERIALIZE WITH NAME PREFIX" WAY

    Let two fields be prefixed with "field"

    <input type="text" name="campoNome">
    <input type="text" name="campoEmail">
    

    Then, we save in a variable the object containing the data of the fields:

    var camposEnviados = $("input[name^='campo']").serialize();
    

    - "MANUALLY BUILD" WAY

    We can construct the array manually (by selecting field by field its value):

    var camposEnviados = [];
    camposEnviados[0] = $('campoNome').val();
    camposEnviados[1] = $('campoEmail').val();;
    

    - SENDING VIA AJAX

    $.ajax({
       type: "POST",
       data: {camposEnviados: camposEnviados},
       url: "index.php",
       success: function(msg)
       {
         $('.resposta').html(msg);
       }
    });
    
        
  • 24.06.2015 / 02:56