php / html Insert multiple data with the same information

0

I'm in need of help because I'm a bit blocked on this issue. I have a form where I put information about a worker. Now I want to change this page to be able to insert several workers. At most up to 16 workers.

Page 1:

    <form method="post" action="inserir4.php"  name="dados-do-cliente" enctype="multipart/form-data">
          <center><h1>Trabalhadores:</h1></center><br>
          <h3>Nome: <input type="text" name="Nome1">  Função: <input type="text" name="Funcao1"><span style="padding-left:150px">
            <Center><h1>Documentos  Trabalhadores : </h1></center>
    <div class="major"> 
     <h2><center> <b><p>Fichas de aptidão medica:</p> </h2>
     </b>  </center>
                <center>     <h3>          Data Validade: 
               <input type="date" name="MedicaValidade"> 
             Anexar Documento: <input type="file" name="MedicaAnexo"></h3></center>
        </div>
<center><button type="submit" style='width: 120px' value="enviar">Gravar</button>
    </center>

Now I want to find a way for this form to be repeated until the user wants it. And whenever a new record is saved in the name of the variable +1 Example page 2:

  <form method="post" action="inserir5.php"  name="dados-do-cliente" enctype="multipart/form-data">
          <center><h1>Trabalhadores:</h1></center><br>
          <h3>Nome: <input type="text" name="Nome2">  Função: <input type="text" name="Funcao2"><span style="padding-left:150px">
            <Center><h1>Documentos  Trabalhadores : </h1></center>
    <div class="major"> 
     <h2><center> <b><p>Fichas de aptidão medica:</p> </h2>
     </b>  </center>
                <center>     <h3>          Data Validade: 
               <input type="date" name="MedicaValidade2"> 
             Anexar Documento: <input type="file" name="MedicaAnexo2"></h3></center>
        </div> 
  <center><button type="submit" style='width: 120px' value="enviar">Gravar</button>
    </center>

At the end of the insert I put this option:

if ($sqlinsert)
{
echo "<script>var r = confirm('Adicionar novo trabalhador?');" .
"if (r == true) {window.open('Trabalhadores3.html','_self','false');}" .
"else {window.open('Equipamentos.html','_self','false');}</script>";
 }

If you want another worker, click Yes and go to the next page if you do not want to go directly to the last page. But I have a big problem because when I go directly to the last page it does not go to get the insertItems but it will get the insert4 that is to add worker4

    
asked by anonymous 11.06.2014 / 16:39

2 answers

2

Hello

You can create a loop in PHP to generate the form's HTML fields, for example:

<form method="post" action="inserir5.php"  name="dados-do-cliente" enctype="multipart/form-data">
<?php 
    $qtd = 16; //A quantidade de formulários
    for ($i=1; $i<=$qtd; $i++) {
?>
<center><h1>Trabalhadores:</h1></center><br>
<h3>Nome: <input type="text" name="Nome<?=$i?>"></h3>
<h3>Função: <input type="text" name="Funcao<?=$i?>"><span style="padding-left:150px"></h3>
<Center><h1>Documentos  Trabalhadores : </h1></center>
<div class="major" align="center"> 
   <h2>Fichas de aptidão medica:</h2>
   <h3>Data Validade: <input type="date" name="MedicaValidade<?=$i?>"></h3> 
   <h3>Anexar Documento: <input type="file" name="MedicaAnexo<?=$i?>"></h3>
</div>
<?php
    }
?>
<center><button type="submit" style='width: 120px' value="enviar">Gravar</button></center>
</form>

Good luck.

    
11.06.2014 / 19:44
0

Well, to solve the problem proposed in your comment, you can do the following:  1 - First, include in the page of your form the newest version of the jQuery library.  2 - Your form will look like this:

<form method="post" action="inserir5.php"  name="dados-do-cliente" enctype="multipart/form-data">
<div id="container">
    <table>
        <tr>
            <td align="center" colspan="2"><h1>Trabalhadores:</h1></td>
        </tr>
        <tr>
            <td>Nome: </td>
            <td><input type="text" name="nome_1"/></td>
        </tr>
        <tr>
            <td>Função: </td>
            <td><input type="text" name="funcao_1"/></td>
        </tr>
        <tr>
            <td align="center" colspan="2"><h1>Documentos  Trabalhadores: </h1></td>
        </tr>
        <tr>
            <td colspan="2"><h2>Fichas de aptidão medica:</h2></td>
        </tr>
        <tr>
            <td>Data Validade: </td>
            <td><input type="date" name="MedicaValidade_1"/></td>
        </tr>
        <tr>
            <td>Anexar Documento: </td>
            <td><input type="file" name="MedicaAnexo_1"/></td>
        </tr>
    </table>
</div>
<p><input type="submit" style='width: 120px' value="Gravar"/></p>
<p><input type="button" name="add" id="add" value="Adicionar"/></p>
</form>

Your JS script to dynamically add elements will look like this:

var i = 1;
$('#add').click(function(){
    i++;
    var table = '<br>';
    table +=     '<table>'
    table +=         '<tr>'
    table +=             '<td align="center" colspan="2"><h1>Trabalhadores:</h1></td>'
    table +=         '</tr>'
    table +=         '<tr>'
    table +=             '<td>Nome: </td>'
    table +=             '<td><input type="text" name="nome_'+i+'"/></td>'
    table +=         '</tr>'
    table +=         '<tr>'
    table +=             '<td>Função: </td>'
    table +=             '<td><input type="text" name="funcao_'+i+'"/></td>'
    table +=         '</tr>'
    table +=         '<tr>'
    table +=             '<td align="center" colspan="2"><h1>Documentos  Trabalhadores: </h1></td>'
    table +=         '</tr>'
    table +=         '<tr id="fichas_aptidao">'
    table +=             '<td colspan="2"><h2>Fichas de aptidão medica:</h2></td>'
    table +=         '</tr>'
    table +=         '<tr>'
    table +=             '<td>Data Validade: </td>'
    table +=             '<td><input type="date" name="MedicaValidade_'+i+'"/></td>'
    table +=         '</tr>'
    table +=         '<tr>'
    table +=             '<td>Anexar Documento: </td>'
    table +=             '<td><input type="file" name="MedicaAnexo_'+i+'"/></td>'
    table +=         '</tr>'
    table +=     '</table>'   
    $('#container').append(table);
})

See the execution of this code in the link below: link

This is a very simple but functional solution for what you need. It is important to remember that some care will be needed to handle the receipt of this data from the PHP side.

Good luck.

    
12.06.2014 / 15:02