1

i'm trying to loop inside my checkbox array and show the value of each element, but its returning 0, and the real value are emails. (lucas.ro****@gmail.com) and (thatian****@gmail.com)

These are my codes:

            <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>

<?php
$Message = "testando";

include("class.phpmailer.php");
include('PHPMailerAutoload.php');
include('class.smtp.php');  


foreach($_POST['box'] as $box){
    if(isset($box)){
        //$dest = $box;
        echo $box;

    } else{
    }
}

?>

Hope someon can help.

Lucas Rodrigues
  • 139
  • 3
  • 13
  • If it is returning `null`, you might want to check what the query is returning. To me this seems to be an issue with ` echo $rowEmail['email'] ?>` – DaGhostman Dimitrov Feb 09 '15 at 17:13
  • 1
    Use `print_r($_POST)` to see if anything arrives at your script. –  Feb 09 '15 at 17:15

1 Answers1

0

At first: SECURITY WARNING! http://php.net/manual/en/function.mysql-connect.php. There is a red box on php.net site, telling that...

mysql_ functions are deprecated. Use for example PDO library: http://php.net/manual/en/pdo.construct.php.

This is a must.


To check whether the post is OK, just use: print_r($_POST). It should display every posted value. However, there is a logic problem in Your code. Take a look here:

foreach($_POST['box'] as $box){
    if(isset($box)){
        //$dest = $box;
        echo $box;
    } else{
    }
}

If ($_POST['box'] = array()), I mean empty array, the foreach will never be reached (as there is no each). So checking isset inside the foreach loop is not needed. You should do like this:

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
}

I hope this is clear and will help you little.


Also remember that checkbox is a little specific. Take a look here: Get $_POST from multiple checkboxes

First answer should be really useful for you.

Community
  • 1
  • 1
Jacek Kowalewski
  • 2,761
  • 2
  • 23
  • 36