views:

121

answers:

3
     while ($row= mysql_fetch_array($result, MYSQL_ASSOC))
{ $id=$row[id];
  $html=<<<html
<tr><td> 
<input style="float:left" type="checkbox" id="$id" name="myBoxes[$id]" value="true">   
<span style="float:left">$row[content]</span>
<span style="color:black;float:right">$row[submitter]</span></td></tr>  
html;
echo $html; 
}


$html=<<<html
</table>
<span onclick="selectAll(true)" style="cursor:pointer;color:black">All</span>
 &nbsp 
<span onclick="selectAll(false)" style="cursor:pointer;color:black">None</span><br/>
<input type="submit" value="Submit"/>
html;
echo $html;

JQuery code:

function selectAll(argument)
{
    $("INPUT[type='checkbox']").attr('checked',argument);
}

PHP code:

<?php foreach ($_POST['myBoxes'] as
    $id => $value)  { echo $value;  
    echo "<br/>";} ?>

Why do I get an error message

Warning: Invalid argument supplied for foreach() in E:\xampp\htdocs\piecework\groupcheck.php on line 2

when I click "None" and "submit", what's the problem?

A: 

$_POST is an associative array that maps strings to strings; hence, $_POST['myBoxes'] is a string. You cannot run a foreach loop over a string.

Thomas
Huh? But he sent it as an array, right? After all, the field has the name `myBoxes[$id]`. Then again, I'm not completely sure whether it really is $id or just replaced by the true ID...
Franz
If, at the point that code gets executed, `$id` has the value `1`, then the field will be named `myBoxes[1]`. That's in no way an array, it's just a string.
Thomas
A: 

If you remove the $id from your html: name="myBoxes[$id]" does it work?

scunliffe
+1  A: 

change this

$row[id]
$row[content]
$row[submitter]

into this:

$row['id']
$row['content']
$row['submitter']
Anas Toumeh