Skip to content

Instantly share code, notes, and snippets.

@antonioanerao
Created August 24, 2017 01:51
Show Gist options
  • Save antonioanerao/dd146af680c0d50c473caf023517d8d2 to your computer and use it in GitHub Desktop.
Save antonioanerao/dd146af680c0d50c473caf023517d8d2 to your computer and use it in GitHub Desktop.
add inputs
<?php
require_once("autoload.php");
$crud = new Crud();
?>
<!DOCTY E html>
<html>
<head>
<title></title>
</head>
<body>
<?php
if(isset($_POST['cadastrar']))
{
$contador = count($_POST['nome']);
$contador_cpf = count($_POST['cpf']);
$total = 0;
while($total < $contador)
{
$insert = $crud -> insert('users', 'nome = ?, cpf = ?', array($_POST['nome'][$total], $_POST['cpf'][$total]));
echo $_POST['nome'][$total] . " adicionado <br>";
$total = $total + 1;
}
}
?>
<form method="post" name="cadastrar" action="http://localhost/inputs/">
<div class="wrap">
<button class="add_campo">Adicionar Participante</button> <br><br>
<div><input type="text" name="nome[]"></div>
<div><input type="text" name="cpf[]"></div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
var max_fields = 5; //maximo de participantes
var wrapper = $(".wrap"); //div wrap
var add_button = $(".add_campo"); //add id
var x = 1;
$(add_button).click(function(e){
e.preventDefault();
if(x < max_fields){
x++;
$(wrapper).append('' +
'<br><br><div><a href="#" class="remover_campo">Remover</a><br>' +
'<input type="text" name="nome[]"/><br>' +
'<input type="text" name="cpf[]"/>' +
'</div>');
}
});
$(wrapper).on("click",".remover_campo", function(e){
e.preventDefault(); $(this).parent('div').remove(); x--;
})
</script>
<br><br> <input type="submit" name="cadastrar" value="Cadastrar no banco">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment