Picking wrong id when selecting line with Checkbox

Asked

Viewed 135 times

0

In this script below I make a loop in rows of a table, where it contains in each row a checkbox and hidden inputs.

The problem is that when I select a few lines with the checkbox, the values corresponding to the checkbox come correct plus the other inputs come as taking data from the unmarked lines

inserir a descrição da imagem aqui

php test.

<form id="form2" name="form2" action="acao.php form=cotacao" method="POST">
<table>    
<tr>
<td><input name="checkbox[]" type="checkbox" value="<?echo $id_produtos>"/>  
</td>
<td><?echo $id_produtos?></td>
<td><input type="hidden" name="nome[]"  value="<?echo $nome?>" /></td>
<td><input type="hidden" name="tipo_serv[]"  value="<?echo $tipo_serv?>" />  
</td>
<td><input type="hidden" name="valor[]"value="<?echo $valor?>" />  
</td> 
</tr><input type="submit" value="Gerar"/>   

acao.php

    if (isset($_POST['checkbox'])) {
    foreach ($_POST['checkbox'] as $key => $value) {
    echo $id_saida = mysql_real_escape_string($value);
         $tipo = mysql_real_escape_string($_POST['tipo_serv'][$key]);
    echo $nome = mysql_real_escape_string($_POST['nome'][$key]);
         $start = mysql_real_escape_string($_POST['start'][$key]);
    echo $valor = mysql_real_escape_string($_POST['valor'][$key]);
         $desconto = mysql_real_escape_string($_POST['desconto'][$key]);
        $tipo_veiculo=mysql_real_escape_string($_POST['tipo_veiculo'[$key]);

2 answers

1


To make the relation using the field id, the attribute name, that has as value checkbox[], shall have as an index the value of the id so that you can read and sign the correct values with the foreach.

In your script you should simply print one echo as index for your array checkbox[], that index shall be the same for the attribute name product. In your case, it would be something like this:

checkbox[<?php echo $id_produtos; ?>]

Below is another example of how it would look, based on a separate script:

<?php

// Ler dados enviados pelo formulario gerado
if(isset($_POST["enviar"])){
    foreach($_POST["checkbox"] as $key=>$value){        
        echo "ID: " . $id . " Nome: " . $_POST["nome"][$key] . "<br/>";
    }    
}


// Conexao
// Eu usei uma tabela local para elaborar este exemplo
$mysqli = new mysqli("localhost", "root", "", "example");
$sql = "SELECT * FROM jogos";
$form = null;

if($query = mysqli_query($mysqli, $sql)){
    while($linha = mysqli_fetch_object($query)){
        $form .= "<input type=\"checkbox\" name=\"checkbox[{$linha->id}]\" value=\"{$linha->id}\"/>{$linha->nome}<br/>";    
        $form .= "<input type=\"hidden\" name=\"nome[{$linha->id}]\" value=\"{$linha->nome}\"/>";
    }    
    
    // Montar o resto do formulário
    
    ?>
    <hr>
    <form method="POST" action="">
    <!-- Resto !-->
    <?php echo $form; ?>
    <!-- Resto !-->
    <input type="submit" name="enviar" value="enviar"/>
    </form>
    
    <?php
}

?>  

Another thing is that I don’t understand exactly how this script of yours is structured, since in one part it is related to cars and in another simply do not know what it is. This was one of the reasons why I had to draw up my own example.

Another recommendation is on functions Mysql who are depreciated, utilize Mysqli or PDO instead of her.


References:

PDO - PHP.net

Mysqli - PHP.net

  • Hello Friend excuse my ignorance , but I couldn’t adapt in my code, to put here https://3v4l.org/L79GG , https://3v4l.org/L2SoW the real code as I use today

  • Your code is too long, for that very reason I isolated and answered the question based on an example I made. But the implementation on yours shouldn’t be difficult, where you have the attributes name="nome[]", name="tipo_serv[]", name="id_produto[]" and others, just put <?echo $id_produtos?> as an index in attributes name, as follows: name="tipo_serv[<?echo $id_produtos?>]", and so on.

  • Hello Amigo I did as suggested most when it only comes the checkbox id. to do so https://3v4l.org/muCMG

  • Look, try this: https://3v4l.org/ErSeU

0

If I understand correctly, you want to send multiple lines, right? The way you’re doing is just sending a form, because you’re putting a form for each line, I think the following might help:

<form id="form2" name="form2" action="acao.php form=cotacao" method="POST">
<table>
<?php
$cmd = "SELECT * FROM produtos where id_transfer = '$id_transfer' AND   
tipo_serv <> 'Regular'  ";

    $produtos = mysql_query($cmd);
    $total = mysql_num_rows($produtos);
    while ($linha = mysql_fetch_array($produtos)) {

 $id_produtos = $linha['id_produtos'];
 $nome = $linha['nome'];
 $valor = $linha['valor'];
 $tipo_serv = $linha['tipo_serv'];
 ?>
<tr>
<td><input name="checkbox[]" type="checkbox" value="<?echo $id_produtos>"/>  
</td>
<td><?echo $id_produtos?></td>
<td><input type="hidden" name="nome[]"  value="<?echo $nome?>" /></td>
<td><input type="hidden" name="tipo_serv[]"  value="<?echo $tipo_serv?>" />  
</td>
<td><input type="hidden" name="valor[]"value="<?echo $valor?>" />  
</td> 
</tr><input type="submit" value="Gerar"/>   
<?php}?>
</table>
</form>

Browser other questions tagged

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