How to send multiple values with inputs of the same name

2
The problem is as follows, I have an html table that is populated within a while in a query to DB, in that table some fields are inputs and I need to enter the data entered in this input into my DB.

I was thinking of getting them in another php file by POST, but that way the names are the same I think it would not work very well.

I do not know if I could explain it right, 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>";
}
    
asked by anonymous 16.11.2017 / 13:53

2 answers

0
  

Solution Example

Here's a way to do it using DOM elements 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 in this way, just make a call to a php file to insert the data into the database. I suggest using Jquery to make this call.

Remembering that this is just an 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 pro your php and do the bank operations, see this tutorial here

I hope I have helped. Anything post here.

    
16.11.2017 / 17:15
0

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

See

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 in with the student's id ( name="media[' . $aluno['id'] . ']" ) in the name and put them in the HTML tag ID to avoid duplicate IDs and break the HTML rule, which in case this happens, leaves the code invalid

I've also added field types, text and hidden . Do not specify them other than invalidate, leave the code less readable.

The hidden field I added to make it easier to wrap data when inserting into the database. And to retrieve and process the received data, do:

$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 retrieve the variables received via post, I used filter_input to ensure a bit more security for the application, but if it does not matter to you, you can use the global variable $_POST with no problems, just keep in mind that is not the most correct way to do it.

    
21.11.2017 / 04:28