I want to diplay comments and in each comment there will be a checkbox:
Now is there anybody who can tell me how use them in php so that when i select
forexample 5checkboxes containing comment each,i will be able to DELETE them All
at the same time.?

Recommended Answers

All 2 Replies

You can use a code like the following:

<form method="post">
<input type="checkbox" name="ids[]" value="1" />
<input type="checkbox" name="ids[]" value="2" />
<input type="checkbox" name="ids[]" value="3" />
<input type="checkbox" name="ids[]" value="4" />
<input type="submit" value="Delete" />
</form>
<pre>
<?php 
if (isset($_POST['ids'])) {
	print_r($_POST['ids']); 
} 
?>
</pre>

If you place the record ID in each checkbox value, you will get them as an array that you can easily use to create your DELETE statement.

if you're using a database, you could use something like this.

<?
/*
this script is for a postgre database, for mysql replace "pg_" with "mysql_"
*/
//connection here
if(count($_POST) > 0)
{
	if(is_array($_POST['chkCommments']))
	{
		$commentpks = "";
		foreach($_POST['chkCommments'] as $commentpk)
		{
			$commentpks .= $commentpk . ", ";
		}
		$commentpks = substr($commentpks, 0, -2); //delete the final ", "
		
		$query = "delete from comments where commentpk in(" . $commentpks . ")";
		pg_query($query);
	} 
}

$query = "select commentpk, comment from comments where commenttype = 'applies to this page'";
$result = pg_query($query);
$total = pg_num_rows($result);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="<? echo $_SERVER['REQUEST_URI']; ?>" method="post">
<?
	for($i = 0; $i < $total; $i++)
	{
	?>
    	<input type="checkbox" value="<? echo pg_result($result, $i, "commentpk"); ?>" />&nbsp;<? echo pg_result($result, $i, "comment"); ?><br />
    <?
	}
?>
</form>
</body>
</html>
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.