Checkbox collecting problem using foreach()

i’m trying to collect the posted values of checkboxes using foreach()

the code:

include("../../../classes/db_connector.php");
$db = new db_connector(); 
$conn = $db->connect();

include "../class/newsletter_manager.php";
$newsletter_manager = new newsletter_manager;
$newsletter_manager->conn = $conn;
$newsletter = $newsletter_manager->list_newsletter();
?>
<form method="post" action="">
<table width="100%" border="0">
<?
foreach($newsletter as $newsletter)
	{?>
		
  <tr>
    <td width="2%"><input name="newsletter_email" type="checkbox" value="<? echo $newsletter['newsletter_id']; ?>" /></td>
    <td width="98%"><? echo $newsletter['newsletter_email']; ?></td>
  </tr>
	<? }
	?>
      <tr>
    <td width="2%">&nbsp;</td>
    <td><input type="submit" name="but" value="send" /></td>
  </tr>

    </table>
    </form>
    
    <?
	if(!empty($_POST[but]))
		{
			foreach($_POST['newsletter_email'] as $singleVar)
				{
					echo $singleVar;
				}
			
		}
	
	?>

the Error

Warning: Invalid argument supplied for foreach() in H:\xampp\htdocs\rockport\modules
ewsletter\admin
ewsletter_subscriber_list.php on line 35


<input name="newsletter_email"

should be


<input name="newsletter_email[]"

notice []

the bug at the second foreach() not the first one

foreach($newsletter as $newsletter)

Don’t use the same variable name for both the array and the single array value. This way, the second loop will give the error because $newsletter isn’t an array anymore.

thanks guido

:slight_smile:
RNEL solved that one for you, now you should noticed the error in the first foreach too.