php/html Insert various data with the same information

Asked

Viewed 1,887 times

0

I am in need of some help because I am a bit stuck on that. I have a form where I put information on a worker. Now I want to change this page to be able to insert several workers. At most up to 16 workers.

Pagina1:

    <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>

and now I want to find a way for that form to be repeated until the user wishes. And whenever a new record is stored 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 I will 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 will not fetch the insertion but it will fetch the insertir4 that is to add worker4

  • Want to reload the page for each new form, or appear on the same page?

  • Show the same page but to store in different places.

  • From what I understand this should be done on the client side. Have some library like Mootools or jQuery?

  • I am using php/html/mysql Workbench . I am seeing if there is such a solution as easy resolution.

  • Excuse my insistence, but I think what you need is not related to php/mysql. Do you need to fetch data from the database for each new form? Or you just need to create a new form on the page for the user to fill in?

  • For example the user has 3 workers and wants to enter them. I want the same form to appear for the 3 workers. The only thing that varies are the places that are stored

  • This is what you seek? http://jsfiddle.net/KJLdB/

  • Yes something like that. When I make a new worker appear on a new page, it is not a very long page. Have a save button at the end of each page and then ask new user?

  • In the comments above you wrote "Show the same page but to store in different places.". But do you want me to save the first form and reload the page with a new form? Then this can be done on the server side. You can join the full HTML with the <form> also?

  • I already put the form. In the worker1 appear name1, functionõ1, medical fitness data1 and worker2 Nome2,function2, medical fitness data2

  • Reload a page with a new form.

  • There is an easier way than creating 16 pages of correct registration?

Show 7 more comments

2 answers

2


Hello!

You can create a PHP loop 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.

  • For example if you only want to put 6 workers. How can I put a button to ask if I want more users?

  • Well, in this case, you’d better use AJAX, jQuery, or something like that. I’ll see if I can find a code I made for something similar. If you find I put for you.

0

Well, to solve the problem proposed in your comment, you can do as follows: 1 - First, include on your form page, the newest version of the jQuery library. 2 - Your form will be as follows:

<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 the 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: http://jsfiddle.net/RibeiroSt/Dn3MT/

This is a very simple but functional solution for what you need. It is important to remember that it will take a certain care to handle receiving this data from the PHP side.

Good luck.

  • and if I create the various pages doesn’t get easier? I’m having a problem opening the pages and saving the data

  • I changed the question one of the changes I made

  • If you are going to enter the same information, but from different people, wouldn’t it be better to have a single PHP page that processes the data and a single form to include the worker data? A JS script would ask if you want to include a new worker. If you choose the 'Yes' option it would call a PHP page to save the information of the worker you entered and clear the form to enter the next person’s data. Otherwise, it would point to another page, perhaps to a later step. That’s it?

  • That’s right. I don’t want to have everything on one page not to make the page huge

  • OK. I’ll think of something like this and put it to you... today it won’t work because I’m very busy, but I’ll do it as soon as possible.

Browser other questions tagged

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