-2
I have a form where the customer clicking the +button adds 4 more fields in the form. But the database only recovers the last four fields added. How can I resolve this?
Follows the script:
<button type="button" onclick="addInput('lines')">+</button>
<script>
var line = 1;
function addInput(divName) {
var newdiv = document.createElement('div');
newdiv.innerHTML = '['+line +']';
newdiv.innerHTML += '<input type="text" name="nomecac[]" id="nomecac" placeholder= "Nome do CAC">';
newdiv.innerHTML += '<input type="text" name="cpfcac[]" id="cpfcac" placeholder= "CPF do CAC">';
newdiv.innerHTML += '<input type="text" name="numerodoc[]" id="numerodoc" placeholder= "Quantidade de documentos à retirar">';
newdiv.innerHTML += '<input type="text" name="numerosigma[]" id="numerosigma" placeholder= "Numero do Sigma">';
document.getElementById(divName).appendChild(newdiv);
line++;
}
addInput('lines');
</script>
With this script, clicking the + button creates the Forms that are passed to the database
Follow the PHP code that captures the data:
$nomecac = $_POST ["nomecac"]; //atribuição do campo "nomecac" vindo do formulário
$cpfcac = $_POST ["cpfcac"]; //atribuição do campo "cpfcac" vindo do formulário para variavel
$numerodoc = $_POST ["numerodoc"]; //atribuição do campo "numerocac" vindo do formulário para variavel
$numerosigma = $_POST ["numerosigma"]; //atribuição do campo "numerosigma" vindo do formulário para variavel
$data = $_POST ["data"]; //atribuição do campo "data" vindo do formulário para variavel
//Gravando no banco de dados !
$mysqli = mysqli_connect("servidor...", ".....", ".....", "....");
$squery = "INSERT INTO agenda (nome, cpf, numerodoc, numerosigma, data) VALUES('$nomecac','$cpfcac','$numerodoc','$numerosigma','$data')";
$resultado = mysqli_query($mysqli,$squery);
There’s already something wrong with your code: repeat id’s.
– Sam
Your code only has a JS that creates an HTML, there is no reference to the database and how this information is obtained. It is imperative to answer the question.
– Inkeliz
I added there friend, if you can give me a light or something else I can give of information, thank you very much!
– Gustavo Arias