How to send multiple values with inputs of the same name

Asked

Viewed 2,192 times

2

The problem is this, I have an html table that is filled inside a while in a BD query, in this table some fields are inputs and I need to insert the data entered in this input in my BD.

I was thinking of getting them in another php file by POST, only that way as the names I don’t think it would go very well.

I don’t know if I could explain it properly, but I’ll leave the code:

while ($linha = mysqli_fetch_array($notas, MYSQLI_ASSOC)) 
{
    $cpfAluno = $linha["Aluno_CPF"];

    $pegaNomeAluno = "SELECT Nome FROM aluno WHERE CPF = '$cpfAluno' ORDER BY Nome";
    $nomeAluno = mysqli_query($con, $pegaNomeAluno); 
    $alunoArray = mysqli_fetch_array($nomeAluno, MYSQLI_ASSOC); 
    $aluno = implode(', ', (array)$alunoArray); 

    echo "<tr>";
        echo "<td>";
            echo $aluno;
        echo "</td>";
        echo "<td>";
            echo "<input id='prova1'  maxlength='2' size='1' name='prova1' class='form-control'>";
        echo "</td>";
        echo "<td>";
            echo "<input id='prova2'  maxlength='2' size='1' name='prova2' class='form-control'>";
        echo "</td>";
        echo "<td>";
            echo "<input id='media'  maxlength='2' size='1' name='media' class='form-control'>";
        echo "</td>";
    echo "</tr>";
}
  • Do you want to save one row at a time ? or the entire table?

  • good to be honest I don’t know what difference it would make by adding everything at once or line by line, but I want to add all the lines that are returned in that while in my bank. Example if there are 10 students in the class will be created 10 lines by while, each row will be a new record in the database, these records will be made when you click a button to submit

  • Difference would be in how to pick up the objects and save, through which way you want to inerate with the database, if each row would have a save button, or if it would have a "save" button to save all the data from the table. If it is the second option, I would indicate organizing the objects in javascript function by taking the elements of the DOM and sending the objects pro php. If so, I can post an example code of how to get these elements.

  • It’s the second way you’ve talked, a button for all the data, please post the code, I’m still learning and know very little about javascript

  • If you can not resolve comment in response, if you can , Mark as solved .

2 answers

1

To send multiple fields with the same name, just treat them as an array. To do this, place brackets in front of the name.

Behold

while ($linha = mysqli_fetch_array($notas, MYSQLI_ASSOC)) 
{
    $cpfAluno = $linha["Aluno_CPF"];

    $pegaNomeAluno = "SELECT Id, Nome FROM aluno WHERE CPF = '$cpfAluno' ORDER BY Nome";
    $nomeAluno = mysqli_query($con, $pegaNomeAluno); 
    $alunoArray = mysqli_fetch_array($nomeAluno, MYSQLI_ASSOC); 
    $aluno = implode(', ', (array)$alunoArray); 

    echo '<tr>';
        echo '<td>';
            echo $aluno;
            echo '<input type="hidden" id="aluno_id_' . $aluno['id'] . '" value="' . $aluno['id'] . '" name="aluno_id[]" class="form-control">';
        echo '</td>';
        echo '<td>';
            echo '<input type="text" id="prova1_' . $aluno['id'] . '"  maxlength="2" size="1" name="prova1[' . $aluno['id'] . ']" class="form-control">';
        echo '</td>';
        echo '<td>';
            echo '<input type="text" id="prova2_' . $aluno['id'] . '"  maxlength="2" size="1" name="prova2[' . $aluno['id'] . ']" class="form-control">';
        echo '</td>';
        echo '<td>';
            echo '<input type="text" id="media_' . $aluno['id'] . '"  maxlength="2" size="1" name="media[' . $aluno['id'] . ']" class="form-control">';
        echo '</td>';
    echo '</tr>';
}

Note that I put brackets filled with the student id in the name (name="media[' . $aluno['id'] . ']") and also put in the ID of the HTML tag to prevent duplicated ID and break the HTML rule that if this happens it leaves the code invalid.

I also added the field types, text and hidden. Not specifying them but invalidating, makes the code less readable.

The field hidden I added it to make it easier for the data to be stored in the database. And to recover and process the received data, please:

$alunoId = filter_input(INPUT_POST, 'aluno_id', FILTER_VALIDATE_INT);
$prova1 = filter_input(INPUT_POST, 'prova1', FILTER_VALIDATE_INT);
$prova2 = filter_input(INPUT_POST, 'prova2', FILTER_VALIDATE_INT);
$media = filter_input(INPUT_POST, 'media', FILTER_VALIDATE_INT);
foreach ( $alunoId as $id ) {
    $prova1[$id]; // Recupera o valor de "prova1" do aluno com o id na váriável $id
    $prova2[$id]; // Recupera o valor de "prova2" do aluno com o id na váriável $id
    $media[$id]; // Recupera o valor de "media" do aluno com o id na váriável $id
    // Faça a lógica do banco de dados aqui.
}

To recover the variables received via post, I used the function filter_input to ensure a little more security for the application, but if it doesn’t matter to you, you can use the global variable $_POST no problems, just keep in mind that is not the most correct way to do.

0

Solution Example

Here I present a way of using the elements of the DOM with javascript. Notice that I have stored this data in a two-dimensional vector where each position is a student record.

[ 
   [
      "nome","nota1","nota2"
   ]

]

After storing the data this way, just make a call to a php file to insert the data in the database. Index use Jquery to make this call.

Remembering that this is just one example that can help you solve the problem.

See worked here.

function getObjetos(){
var table = document.getElementById('table');
debugger;
var results = [];
for(var i =0 ; i < table.children.length ;i++){

  if(table.children[i].tagName == 'TBODY'){
    var tbody = table.children[i];
    for(var c =0;  c< tbody.children.length ; c++){
      if(tbody.children[c].nodeName=='TR'){
        var tr = tbody.children[c];
        var nome = tr.children[0].textContent;
        var n1 = tr.children[1].children[0].value;
        var n2 = tr.children[2].children[0].value;
        results.push( [nome,n1,n2]);
        
      }
    }
  }
}

console.log(results);
return false;
}
<form name="notas">
<table border="1" id="table">
  <thead>
    <tr>
      <th>Nome</th>
      <th>Nota1</th>
      <th>Nota2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Marcos</td>
      <td><input type="text" name="nota2" id="nota1" /></td>
      <td><input type="text" name="nota2" id="nota2" /></td>
    </tr>
    <tr>
      <td>Alexandre</td>
      <td><input type="text" name="nota2" id="nota1" /></td>
      <td><input type="text" name="nota2" id="nota2" /></td>
    </tr>
  </tbody>
</table>
<input type="button" value="salvar" onclick="getObjetos()"/>
</form>

To make the call your php file and do the bank operations, see this tutorial here

I hope I helped. Anything post here.

Browser other questions tagged

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