Hello all. I have an HTML page that displays a list of comments left on a site. The administrator needs to go in and put checks next to the comments he wants to delete, and click submit.

echo "<td width=\"75\" align=\"center\"><input type=\"checkbox\" name=\"toApprove[]\" id=\"approve\" value=\"approve\" />";

echo "<td width=\"75\" align=\"center\"><input type=\"checkbox\" name=\"toDelete[]\" id=\"delete\" value=\"delete\" />";

echo "<input type=\"submit\" name=\"approve\" value=\"Approve\" /></td>";

echo "<td><input type=\"submit\" name=\"delete\" value=\"Delete\" /></td></form></tr>";


The submit goes to a php page with the code that should do the deleting. But that's where I am stuck.

I cannot seem to get the data on the state of the checkbox to go to the php page.

It should work out that the admin can, for example, put a check on messages 1 and 3, hit submit, and they are deleted for the file. Leaving only messages 1 and 4.

I really appreciate and help I can get. Thanks in advance.

Your checkboxes need to be an array that php can loop through. All should have a name="chkField[]" or something similar. The value is the only thing that should be different between one checkbox and the next. Then in the form processor,

foreach($_POST['chkField'] as $value){
$sql="DELETE FROM TABLE WHERE id=$value";
$result=mysql_query($sql);
}

Your checkboxes need to be an array that php can loop through. All should have a name="chkField[]" or something similar. The value is the only thing that should be different between one checkbox and the next. Then in the form processor,

foreach($_POST['chkField'] as $value){
$sql="DELETE FROM TABLE WHERE id=$value";
$result=mysql_query($sql);
}

Thanks for the info. When I try to run this the only output I get is 'Array'. I try to echo the $chkfield and $value just to test what the output is, and it always comes back "Array."

Your checkboxes need to be an array that php can loop through. All should have a name="chkField[]" or something similar. The value is the only thing that should be different between one checkbox and the next. Then in the form processor,

foreach($_POST['chkField'] as $value){
$sql="DELETE FROM TABLE WHERE id=$value";
$result=mysql_query($sql);
}

Thanks for the info. When I try to run this the only output I get is 'Array'. I try to echo the $chkfield and $value just to test what the output is, and it always comes back "Array."

I'm sure I am making this harder than it is. I guess I have just stared at it too long, and it all looks the same now.

if you are trying to echo an array you should use print_r($array).

[offtopic]
I see you use several shashes in one line of code:

echo "<td width=\"75\" align=\"center\"><input type=\"checkbox\" name=\"toApprove[]\" id=\"approve\" value=\"approve\" />";

Why not do it this way?

echo '<td width="75" align="center"><input type="checkbox" name="toApprove[]" id="approve" value="approve" />';

Note the difference: the ' and the "
[/offtopic]

Ontopic:
If your checkbox is not checked, it does not POST the value..

By the way the code posted above is not injection proof, use this code instead:

foreach($_POST['chkField'] as $value){
	$sql="DELETE FROM TABLE WHERE id='".mysql_real_escape_string($value)."'";
	$result=mysql_query($sql);
}foreach($_POST['chkField'] as $value){
	$sql="DELETE FROM TABLE WHERE id='".mysql_real_escape_string($value)."'";
	$result=mysql_query($sql);
}

And no injection can be applied.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.