Pass an array of a form to PHP via AJAX

Asked

Viewed 64 times

3

I have this form:

jQuery

(function($) {
  AddTableRow = function() {

    var newRow = $("<tr>");
    var cols = "";

    cols += '<td><input type="text" name="dados[][nome]" id="dados[][nome]" class="form-control" value=""></td>';
    cols += '<td><input type="text" name="dados[][nome]" id="dados[][nome]" class="form-control" value=""></td>';
    cols += '<td><input type="text" name="dados[][nome]" id="dados[][nome]" class="form-control" value=""></td>';
    cols += '<td><input type="text" name="dados[][nome]" id="dados[][nome]" class="form-control" value=""></td>';
    cols += '<td>';
    cols += '<button onclick="RemoveTableRow(this)" type="button"><span class="fa fa-minus"></button></button>';
    cols += '</td>';

    newRow.append(cols);
    $("#convidados-table").append(newRow);

    return false;
  };
})(jQuery);

To better understand, I modified the code... How I would make this adjustment to array?

  • The ID you can take, it can never repeat itself on the page. When you clone the line will duplicate the ID.

1 answer

2


You can do so, with a counter on JS

<input type="text" name="dados[][nome]" class="form-control" value="">
<input type="text" name="dados[][faixaetaria]" class="form-control" value="">
<input type="text" name="dados[][telefone]" class="form-control" value="">
<input type="text" name="dados[][email]" class="form-control" value="">

And in jQuery you can do the same in HTML, without ID and with name equal form.

var cont = 1;
$("#adicionar_convidado").click(function() {
   cont++;
   var campos_novos = "\
   <div class='dep_fc' id='dep_fc'> \
      <div class='col-md-12' style='padding: 5px;'> \
         <div class='col-md-3'> \
            <input type='text' name='dados[nome]["+cont+"]' placeholder='Nome' class='form-control'></div> \
            <div class='col-md-2'><input type='text' name='dados[faixaetaria]["+cont+"]' placeholder='Faixa Etária' class='form-control'></div> \
            <div class='col-md-3'><input type='text' name='dados[telefone]["+cont+"]' placeholder='Telefone' class='form-control'></div> \
            <div class='col-md-3'><input type='text' name='dados[email]["+cont+"]' placeholder='E-mail'  class='form-control'></div> \
            <div class='col-md-1'><button class='btn btn-default remove' type='button'><span class='fa fa-minus'></span></button></div> \
         </div> \
      </div> \
   </div>";
   $("#dep").append(campos_novos);
});

In my case I would return:

"dados" => array:4 [
    "nome" => array:2 [
      0 => "A"
      1 => "E"
    ]
    "faixaetaria" => array:2 [
      0 => "B"
      1 => "F"
    ]
    "telefone" => array:2 [
      0 => "C"
      1 => "G"
    ]
    "email" => array:2 [
      0 => "D"
      1 => "H"
    ]
]

Hence the PHP would be:

foreach($dados['nome'] as $key => $value){
    $fe       = $dados['faixaetaria'][$key];
    $email    = $dados['email'][$key];
    $telefone = $dados['telefone'][$key];
    $nome     = $value;
}
  • So it won’t work, I need the return to be: array ( 0 ( name = name, email = email, phone = phone, age = age) (1 (name = name, email = email, phone = phone, age = age) and so on

  • The way you want it, you’ll have to create an accountant. If I do it my way you will receive in PHP with a Foreach only in one field and use the Index to catch the others.

  • How would you return? In this case

  • In this last edition, it will return in separate arrays so I need to create the counter inside the Divs, that is, the first line is 0, the others, 1, 2, 3... wo would be right, note that it is table and not div. So I changed my question

  • I did it the way you need it, with an accountant.

  • I did a little different, but I used the logic of cont =1, cont++, to count when I included Row, it worked, thanks! : ) A prosperous 2017 for you.

  • In this case it is easier to insert using codeigniter: [data] => Array ( [0] => Array ( [name] => teste1 [failurery] => 33 [telephone] => [email protected] [email] => (41) 99 99999999 )

  • 1

    Got it. EEEEE Code Igniter, just giving you a headache.

Show 3 more comments

Browser other questions tagged

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