Passing values from checkbox

Asked

Viewed 1,141 times

2

Good afternoon,

I need to pass the values selected from a checkbox to a page through a form by Post and save them in a database, but I’m not getting, could help me?

Follows the code:

Form:

<form class="form-horizontal" method="POST" action="operacoes.php?action=GeraProva">
            <div align="center" style="margin-top:200px;">
                <div class="control-group">
                    <label class="labelTrab" for="txtNmMod">Módulo:</label>
                    <input id="txtNmDis" name="txtNmMod" type="text" class="input-mini" value="<?php echo $dsmodulo ?>" disabled="disabled"><br/>
                    <?php if($valProvas > 0) {?>
                        <label class="labelTrab" for="txtNrProva">Prova:</label>
                        <input id="txtNmDis" name="txtNrProva" type="text" class="input-mini" value="<?php echo $cdprova ?>" disabled="disabled"><br/>
                    <?php }?>
                    <table border='1'>
                    <tr>
                        <th>Selecionar</th><br/>
                        <th>ID</th><br/>
                        <th>Descrição</th><br/>
                        <th>Disciplina</th><br/>
                    </tr>
                    <?php 
                        while($row = mysql_fetch_array($var)) 
                        {
                            $ar = $row["cod_disc"];
                            $aux = mysql_query("select nome_disc from tb_disciplinas where cod_disc = '$ar'");
                            $ftc1 = mysql_fetch_array($aux);
                            $nmdisc = $ftc1["nome_disc"];
                            $id = $row["cdpergunta"];
                            echo "<tr>";
                            echo "<td><input type='checkbox' name='checkPerg[]'/></td><br/>";
                            echo "<td>" . $row["cdpergunta"] . "</td><br/>";
                            echo "<td>" . $row["dspergunta"] . "</td><br/>";
                            echo "<td>" . $nmdisc . "</td><br/>";
                            echo "</tr>";
                        }
                    ?>
                    </table>
                </div>
                <div style="margin-top:5px;">
                        <button id="singlebutton" name="btnPesquisa" class="btn btn-primary">Gerar Prova</button>
                        <a href="lista.php?action=EnviaDadosLista"><input type="button" id="singlebutton" name="btnVoltar" class="btn btn-primary" value="Voltar"/>
                </div>
            </div>
        </form>

PHP:

$id = $_GET['v'];

    $nmodulo = $_POST['txtNmMod'];
    if(isset($_POST['checkPerg']))
    {
        echo "<script>alert('Pera!')</script>";
        if(count($_POST['checkPerg']) > 0)
            foreach($_POST['checkPerg'] as $item)
                echo $item."<br/>";
    }
    if(isset($_POST['txtNrProva']))
    {
        $cdprova = $_POST['txtNrProva'];
    }
    else
    {
        //echo "<script>alert('Pode Voltar!')</script>";
        echo "<meta http-equiv='refresh' content='0, url=lista.php?v=$nmodulo&r=$nrqts&action=GeraLista'>";
    }

You could help me in this situation?

Thank you in advance.

1 answer

3


You’re not passing any value in his checkbox

echo "<td><input type='checkbox' name='checkPerg[]'/></td><br/>";

Put something like this:

echo "<td><input type='checkbox' name='checkPerg[]' value='sim'/></td><br/>";

or

echo "<td><input type='checkbox' name='checkPerg[]' value='" . $seuValor . "'/></td><br/>";
  • That way I would pass one value at a time, right? How can I pass an entire array?

  • Yes, one for each checkbox

  • But the checkbox would not be checked or checked? In case it doesn’t make much sense to get value, it would be more interesting to get checked (equal to true or false).

  • This via javascript, at the time of the request you need the value to see if it’s been checked.

  • In your case if ($item == 'sim') //checked

  • It turned out I didn’t need the whole array, only the first item , the rest picks up with query. Thanks for the collaboration.

  • @Maiconcarraro how I could solve this my http://answall.com/questions/178790/como-enviarcheckbox-com-names-diferentes-para-o-mysql

Show 2 more comments

Browser other questions tagged

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