Array with HTML and Jquery

Asked

Viewed 55 times

0

I have a form, where the fields correspond to the amount registered in the database. I am bringing as follows:

<?php
....
$contar = $metodos->contarCadastro($chave);
$num = 1;
for($c = 1; $c <= $contarPaxes; $c++){
?>
    <div align='left'><label>Nome Completo:</label> <input type='text' name='NomeCompleto[]' id='nome' class='form-control' maxlenght='100'></div>
                <div align='left'><label>E-mail:</label> <input type='text' name='Email[]' id='email' class='form-control' maxlenght='100'></div>
                <div align='left'><label>Data de Nascimento:</label> <input type='text' name='DataNascimento[]' id="date" class='form-control' maxlenght='100'></div>
                <div align='left'><label>CPF:</label> <input type='text' id="cpf" name='CPF[]' class='form-control' maxlenght='100'></div>
<?php } ?>

And the jquery:

$("#btnCadastrar").on("click", function() {
  var nome = $("#nome").val();
  var email = $("#email").val();
  var data = $("#date").val();
var cpf =  $("#cpf").val();

  $.post("cadastrar.php", {Nome: nome, Email: email, data: data, CPF: cpf }, function() { 
  $(document).ready(function () {
    $('#myModa3').modal();
  });
});
});

But if you have to register 03 people, it only registers one. How do I so that in the case of array, register by jquery?

  • how is getting your php?

  • With the traditional mode. mysqli_query($connection, "INSERT INTO...");

1 answer

0

Remember that ID is unique per element.

Quick and easy suggestion:

Create a <form> with any ID and put these inputs inside this <form>.

After that, in your JS function do the following:

$("#btnCadastrar").on("click", function() {
    var formElements = $("#idDoSeuForm").serialize();


  $.post("cadastrar.php?"+formElements, function() { 
  $(document).ready(function () {
    $('#myModa3').modal();
  });
});
});

of course you turn into JSON before sending.

Browser other questions tagged

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