2015-02-09 73 views
1

我想循環內我的複選框數組並顯示每個元素的值,但其返回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> 

<?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{ 
    } 
} 

>

希望有人能幫助。

+0

如果它返回'null',則可能需要檢查查詢返回的內容。對我來說這似乎是一個問題''? echo $ rowEmail ['email']?>' – 2015-02-09 17:13:52

+1

使用'print_r($ _ POST)'來查看是否有任何東西到達您的腳本。 – 2015-02-09 17:15:00

回答

0

起初:安全警告!http://php.net/manual/en/function.mysql-connect.php。有一個php.net網站上的紅色框,告訴...

mysql_函數已被棄用。使用例如PDO庫:http://php.net/manual/en/pdo.construct.php

這是必須的。


要檢查帖子是否正常,請使用:print_r($_POST)。它應該顯示每個發佈的值。但是,您的代碼中存在邏輯問題。看看這裏:

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

如果($_POST['box'] = array()),我的意思是陣列,在foreach永遠不會達到(因爲沒有每個)。 因此,不需要在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 
} 

我希望這是明確的,並會幫助你一點。


還記得複選框是有點特定的。看看這裏:Get $_POST from multiple checkboxes

首先回答應該是真的對你有用。