Php 显示复选框数组值

Php 显示复选框数组值,php,arrays,checkbox,show,Php,Arrays,Checkbox,Show,我试图在我的复选框数组中循环并显示每个元素的值,但它返回0,真正的值是电子邮件。(lucas.ro****@gmail.com)和(thatian****@gmail.com) 这是我的密码: <form method="POST" action="testeEmail.php"> <table id="tableEmail" class="table table-striped table-bordered">

我试图在我的复选框数组中循环并显示每个元素的值,但它返回0,真正的值是电子邮件。(lucas.ro****@gmail.com)和(thatian****@gmail.com)

这是我的密码:

            <form method="POST" action="testeEmail.php">
            <table id="tableEmail" class="table table-striped table-bordered">
                <tr>
                  <td>Email</td>
                  <td width="5%">Enviar?</td>
                  <td class="centro">Já recebido</td>
                  <td width="10%"> <button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-send"></span> Enviar (Os útimos não enviados)</button></td>
                </tr>
                <?
                include("conexao.php");
                mysql_query("SET NAMES 'utf8'");
                mysql_query("SET character_set_connection=utf8");
                mysql_query("SET character_set_client=utf8");
                mysql_query("SET character_set_results=utf8");
                $resultEmail = mysql_query("SELECT id,email,recebido FROM Newsletter");
                if (mysql_num_rows($resultEmail) > 0) {
                    while ($rowEmail = mysql_fetch_assoc($resultEmail)){?>
                       <tr>
                            <td><? echo $rowEmail['email'] ?></td>
                            <td class="centro">
                                <input type="checkbox" name="box[]" value="<? echo $rowEmail['email'] ?>" <? if($rowEmail['recebido'] == 1){}else{ echo "checked"; } ?>>
                            </td>
                            <td class="centro"><? if($rowEmail['recebido'] == 1){ echo "<font color='green'>Sim</font>";}else{ echo "<font color='#d9534f'>Não</font>"; } ?></td>
                            <td>
                                <a href="deletarEmail.php?idEmail=<? echo $rowEmail['id'] ?>" class="btn btn-danger"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> DELETAR</a>
                            </td>
                       </tr>
                    <?}
                }
                 mysql_free_result($resultEmail);
                 mysql_close($conecta);?>
            </table>
        </form>

电子邮件
嫉妒?
贾里切比多
Enviar(Osútimos não enviados)
>

首先:安全警告。在php.net网站上有一个红色框,告诉你

mysql\函数已弃用。例如使用PDO库:

这是必须的


要检查帖子是否正常,只需使用:
print\r($\u post)
。它应该显示每个发布的值。但是,代码中存在逻辑问题。请看这里:

foreach($_POST['box'] as $box){
    if(isset($box)){
        //$dest = $box;
        echo $box;
    } else{
    }
}
如果
($\u POST['box']=array())
,我的意思是数组,则永远不会到达foreach(因为没有each)因此无需在foreach循环内检查isset。你应该这样做:

if(isset($_POST['box']) && is_array($_POST['box'])) {
    foreach($_POST['box'] as $box){
        var_dump($box); // Always use var_dump for testing echo...
    } 
else {
    // box'es were not posted
}
我希望这是明确的,并将帮助你一点


还要记住,复选框有点特殊。请看这里:


第一个答案应该对您非常有用。

如果它返回的是
null
,您可能需要检查查询返回的内容。在我看来,这似乎是
使用
print\r($\u POST)
查看脚本中是否有任何内容。
if(isset($_POST['box']) && is_array($_POST['box'])) {
    foreach($_POST['box'] as $box){
        var_dump($box); // Always use var_dump for testing echo...
    } 
else {
    // box'es were not posted
}