duanfen1312 2013-08-09 14:51
浏览 67

当复选框被选中时,从表中获取值

I have this code to show my table:

 <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
   <table cellspacing='0'>
 <?php      
    if(isset($_GET["ordem"])){

    if($_GET["ordem"] == 'descendente'){
        echo "<thead><tr><th><a title='Ordenar por título' href='visualizarVoucher.php'>Utilizador</a></th>";
        echo "<th>Email</th>";
        echo "<th>Voucher</th>";
        echo "<th>Categoria</th>";
        echo "<th>Preço</th>";
        echo "<th>Confirmação</th>";
        echo "<th>Enviar mail</th>";
        echo "</tr></thead>";
    }
    elseif($_GET["ordem"] == 'ascendente'){
            echo "<thead><tr><th><a title='Ordenar por título' href='visualizarVoucher.php?ordem=descendente'>Utilizador</a></th>";
            echo "<th>Email</th>";
            echo "<th>Voucher</th>";
            echo "<th>Categoria</th>";
            echo "<th>Preço</th>";
            echo "<th>Confirmação </th>";
            echo "<th>Enviar mail</th>";
        echo ("</tr></thead>");
    }                       
}
else{
    echo "<thead><tr><th><a title='Ordenar por título' href='visualizarVoucher.php?ordem=ascendente'>Utilizador</a></th>";
    echo "<th>Email</th>";
    echo "<th>Voucher</th>";
    echo "<th>Categoria</th>";
    echo "<th>Preço</th>";
    echo "<th>Confirmação</th>";
    echo "<th>Enviar mail</th>";
    echo("</tr></thead>");
}              


    while($stmt->fetch()){
            echo("<tbody>");
            echo("<tr><td>$nomeUser</td>");
            echo("<td>$email</td>");
            echo("<td>$nomeVoucher</td>");
            echo("<td>$categoria</td>");
            echo("<td>$preco</td>");
            echo("<td>$confirmacao</td>");
            $content = file_get_contents($file,$filePDF);
            echo("<td><INPUT TYPE='checkbox' NAME='mail[]' multiple='yes'></td>");
            echo("</tr>");
            echo("</tbody>");
    }$stmt->close();     ?>

I have a checkbox in my table and I would like to know how can I get the values of each rows from the table when i selected the checkbox. I want to send email when the user selected multiple checkbox. Thanks

  • 写回答

1条回答 默认 最新

  • duanjiao4763 2013-08-09 15:15
    关注

    It's possible that the code above is a snippet of a larger page, but if not:

    • You aren't actually wrapping your input elements in an HTML form tag. Doing so will cause the user agent (presumably a browser) to treat each input tag as something to be submitted to your backend form.

    • You should wrap the tables in a table element; tbody is a child of a table.

    Regardless of the above:

    • It looks like your PHP code above will render the entire tbody each time the statement fetches a new row, which is a bit weird. I'd assume you only want to render a row containing mail options?

    • To the best of my knowledge, there is no multiple attribute allowed in a checkbox element. You may be thinking of the select element.

    • You are not setting a value attribute on your checkbox input tag. If the user actually submits a form, you'll either get a set of empty mail[] variables, or nothing at all, I'm not actually sure which.

    Consider the code below: Note that the checkboxes have the same name attribute, but different values.

    <form method="post">
        <table>
            <tbody>
                <tr>
                    <td>
                        <input type="checkbox" name="mail[]" value="val1" id="mail-val1" />
                        <label for="mail-val1">Value 1</label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" name="mail[]" value="val2" id="mail-val2" />
                        <label for="mail-val2">Value 2</label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" name="mail[]" value="val3" id="mail-val3" />
                        <label for="mail-val3">Value 3</label>
                    </td>
                </tr>
            </tbody>
        </table>
        <input type="submit" />
    </form>
    

    Given this form, if the user selects all three of the checkboxes and submits, your server will receive a POST request with the payload:

    mail[]=val1&mail[]=val2&mail[]=val3
    

    Depending on how PHP parses that response (it's been about a decade since I've dealt with PHP), you'll probably have an array variable accessible to your application:

    # mail == ["val1", "val2", "val3" ]
    

    Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?