How do I get the value of each checkbox that is inside a loop?

1

Good morning guys, I need help :( It is as follows, I need to delete several records at once so I need to select the ID of each record, so I did an input with a checkbox inside the loop like this:

<form method="POST" id="formDeletarMsg" action="deletarMsg.php">
   <input type="checkbox" name="msg[]" value="<?php echo $idMsg; ?>">                          
</form>

The submit button is out of the form, I'm using javascript because it will stay in the section footer.

And I'm using a foreach to display the data of each checkbox, it even works, only it only displays the first checked checkbox, if I mark the second or third it does not display anything, if I mark the first one it displays:

if(isset($_POST['msg'])){
$msg = $_POST['msg'];
foreach($msg as $key => $value){
    echo $key . "<br>";
    echo $value;
}
var_dump($value);
}
    
asked by anonymous 10.11.2016 / 14:21

1 answer

3

Well, I'm not sure what to do with the form, so I was only displaying the first checked checkbox, the form has to be out of the while but the input has to be inside. SOLVED !!!

<form method="POST" id="formDeletarMsg" action="deletarMsg.php">
                <?php
                    while($row = mysqli_fetch_array($sqlMps)):
                        $idMsg = $row['idMsg'];
                        $assunto = utf8_encode($row['assunto']);
                        $usuario = $row['usuario'];
                        $visualizou = $row['visualizou'];
                        $dataMsg = utf8_encode($row['dataMsg']);

                ?>
                <li>
                    <span><a <?php if($visualizou == 0){ echo "class='negrito'";}?> href="visualizarMp.php?id=<?php echo $idMsg; ?>"><?php echo substr($assunto, 0, 30); ?></a>   por <strong><?php echo $usuario; ?></strong> - <?php echo $dataMsg; ?></span>
                    <div class="right">

                            <input type="checkbox" name="deletar[]" value="<?php echo $idMsg; ?>">


                    </div>
                </li>

                <?php
                    endwhile;
                ?>
                 </form>
    
10.11.2016 / 14:41