Jump to content
Fórum Script Brasil
  • 0

(Resolvido) Manter Checkbox marcada ao submeter formulário com get


dinohills

Question

Boa tarde !!

Estou montando uma tabela onde cada linha possuí uma checkbox, e ao submeter o formulário gostaria de manter as checkbox que o usuário selecionou marcadas para não precisar ficar remarcando toda vez que o formulário for submetido.

Mas o que acontece é o seguinte. Se eu marco apenas uma checkbox e envio o formulário essa checkbox não fica marcada, mas se eu marcar duas checkbox todas as checkbox ficam marcadas depois de submeter o fomulário.

Montei este exemplo simples do que eu estou tendo dificuldades.

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        foreach ($_GET['box'] as $check => $valor) {            
            if ($check) {
                $marcado = "checked";
            }else{
                $marcado = "";
            }
        }
        ?>
        <form action="" method="get">
            <table border="1" width="300" align="center">
                <thead>
                <th>&nbsp;</th>
                <th>Item - A</th>
                <th>Item - B</th>
                </thead>
                <tbody align="center">
                    <tr>
                        <td><input type="checkbox" name="box[]" value="1" <?php echo $marcado; ?> </td>
                        <td>Item - 1</td>
                        <td>Item - 1</td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="box[]" value="2" <?php echo $marcado; ?> </td>
                        <td>Item - 2</td>
                        <td>Item - 2</td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="box[]" value="3" <?php echo $marcado; ?> </td>
                        <td>Item - 3</td>
                        <td>Item - 3</td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="box[]" value="4" <?php echo $marcado; ?> </td>
                        <td>Item - 4</td>
                        <td>Item - 4</td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="box[]" value="5" <?php echo $marcado; ?> </td>
                        <td>Item - 5</td>
                        <td>Item - 5</td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td align="center" colspan="3"><input type="submit" value="envia"/></td>
                    </tr>
                </tfoot>
            </table>
        </form>        
    </body>
</html>

Acho que não falta muita coisa pra acertar !!

Bom se alguém tiver uma solução ou ideia qualquer coisa pra ajudar agradeço :D

Obrigado.

Edited by dinohills
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Consegui ajuda em outro forum, usando este código.

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form action="" method="POST">
            <table border="1" width="300" align="center">
                <thead>
                <th>&nbsp;</th>
                <th>Item - A</th>
                <th>Item - B</th>
                </thead>
                <tbody align="center">
                    <?php
                    $tableCells = array(
                        array('Item 1', 'Item 2'),
                        array('Item 1', 'Item 2'),
                        array('Item 1', 'Item 2'),
                        array('Item 1', 'Item 2'),
                        array('Item 1', 'Item 2'),
                    );
                    $i = 0;
                    foreach ( $tableCells as $cell ): ?>
                            <tr>
                                <td>
                                        <input
                                                type="checkbox"
                                                name="box[<?php echo $i; ?>]"
                                                value="1"
                                                <?php if ( isset( $_POST['box'][$i++] ) ) echo 'checked="checked"'; ?>
                                        />
                                </td>
                                <?php foreach ( $cell as $value ): ?>
                                        <td><?php echo $value; ?></td>
                                <?php endforeach; ?>
                            </tr>
                    <?php endforeach; ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td align="center" colspan="3"><input type="submit" value="envia"/></td>
                    </tr>
                </tfoot>
            </table>
        </form>        
    </body>
</html>

Obrigado.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652.1k
×
×
  • Create New...