Jump to content

[SOLVED] delete entrys, using checkboxes?


DeanWhitehouse

Recommended Posts

how can i have people select entry's to delete with checkboxes, how would i check to see what entry to delete?

this is what i have so far

<?php
if(isset($_POST['delete']))
			{
			$newname = mysql_real_escape_string($_POST['newname']);
			$newcontent = mysql_real_escape_string($_POST['newcontent']);
			$runame = mysql_real_escape_string($_POST['ru_name']);
			$rucont = mysql_real_escape_string($_POST['ru_content']);
				if ($newname && $newcontent)
				{
				mysql_query("INSERT INTO `darkflame_news` (id,  name, content, time) VALUES( '','$newname','$newcontent', '$date')")
				or die('Error ' . mysql_error());
				echo "<tr><td>News Saved</td></tr>";

				if ($runame && $rucont)
					{
					mysql_query("INSERT INTO `darkflame_news_ru`(id,  name, content, time) VALUES( '','$runame','$rucont', '$date')")
					or die('Error ' . mysql_error());
					}
				}

				elseif (!newname.newcontent)
				{
				echo "<tr><td>Please Fill In The Required Fields</td></tr>";
				}
			}

if(isset($_GET['delete_news']))
{
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<table class="news_profile"><tr><th>Delete News</th><td class="delete">Delete</td></tr><tr><td align="left">
<?php
//No ID passed to page, display user list:
if($_SESSION['lang_en'] == true)
{
$query = "SELECT id, name, content, time FROM darkflame_news ORDER BY id DESC LIMIT 5";
}
else
{
$query = "SELECT id, name, content, time FROM darkflame_news_ru ORDER BY id DESC LIMIT 5";
}
$result = mysql_query($query) or die("Error:" . mysql_error());


if (mysql_num_rows($result) > 0) 
{
	while ($row = mysql_fetch_assoc($result)) {
	$content = $row['content'];
	$cont = wordwrap($content, 118, "<br />", true);
	echo "<b>".$row['time']."</b>"; ?> - <a href="?news_id=<?php echo $row['id']; ?>"><?php echo $row['name']; ?></a></td><td><input type="checkbox" name="delete<?php echo $row['id']; ?>"></td></tr><tr align="left"><td><?php echo nl2br(stripslashes($cont));; ?></td></tr>
	<?php
	}
}
if($_SESSION['lang_en'] == true)
{
		if (mysql_num_rows($result) < 1) {
		echo "No News To Display. <br>Keep Checking Back<br>";
		}

		if (mysql_num_rows($result) > 5) {
		echo "<br><a href='?news_all'>All News</a>";
		}			
}
if($_SESSION['lang_ru'] == true)
{
		if (mysql_num_rows($result) < 1) {
		echo "(in russian)No News To Display. <br>Keep Checking Back<br>";
		}

		if (mysql_num_rows($result) > 5) {
		echo "<br><a href='?news_all'>More News</a>";
		}			
}
?>
</td></tr><tr><td><input type="submit" name="delete" value="Delete Selected"></td></tr></table>
<?php
exit();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/105774-solved-delete-entrys-using-checkboxes/
Share on other sites

the entries need to be in an array, so it would look like this:

 

Note the the [] in the name, the is what creates your array

<input type="checkbox" value="1" name="checkB[]" />
<input type="checkbox" value="2" name="checkB[]" />
<input type="checkbox" value="3" name="checkB[]" />

 

then in your php:

foreach($_POST['checkB'] as $checked){
      mysql_query("DELETE FROM tableName WHERE id='$checked'");
}

ok, i am using this code and it isn't deleting anything

<?php
			if(isset($_POST['delete']))
			{
				foreach($_POST['checkB'] as $checked)
				{
				mysql_query("DELETE FROM darkflame_news WHERE id='{$checked}'") or die("Error:" . mysql_error());
				}
			}

if(isset($_GET['delete_news']))
{
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<table class="news_profile"><tr><th>Delete News</th><td class="delete">Delete</td></tr><tr><td align="left">
<?php
//No ID passed to page, display user list:
if($_SESSION['lang_en'] == true)
{
$query = "SELECT id, name, content, time FROM darkflame_news ORDER BY id DESC LIMIT 5";
}
else
{
$query = "SELECT id, name, content, time FROM darkflame_news_ru ORDER BY id DESC LIMIT 5";
}
$result = mysql_query($query) or die("Error:" . mysql_error());


if (mysql_num_rows($result) > 0) 
{
	while ($row = mysql_fetch_assoc($result)) {
	$content = $row['content'];
	$cont = wordwrap($content, 118, "<br />", true);
	echo "<b>".$row['time']."</b>"; ?> - <a href="?news_id=<?php echo $row['id']; ?>"><?php echo $row['name']; ?></a></td><td><input type="checkbox" value="1" name="checkB[]" /></td></tr><tr align="left"><td><?php echo nl2br(stripslashes($cont));; ?></td></tr>
	<?php
	}
}
if($_SESSION['lang_en'] == true)
{
		if (mysql_num_rows($result) < 1) {
		echo "No News To Display. <br>Keep Checking Back<br>";
		}

		if (mysql_num_rows($result) > 5) {
		echo "<br><a href='?news_all'>All News</a>";
		}			
}
if($_SESSION['lang_ru'] == true)
{
		if (mysql_num_rows($result) < 1) {
		echo "(in russian)No News To Display. <br>Keep Checking Back<br>";
		}

		if (mysql_num_rows($result) > 5) {
		echo "<br><a href='?news_all'>More News</a>";
		}			
}
?>
</td></tr><tr><td><input type="submit" name="delete" value="Delete Selected"></td></tr></table>
<?php
exit();
}
?>

value="1" for your checkbox gives all your checkboxes a 1 value, so when you sql runs you get:

 

DELETE FROM darkflame_news WHERE id='1'

 

You need to give the value of your checkboxes the IDs of the records you want to delete.

 

<?php
<input type="checkbox" value="<?php echo $row['id']; ?>" name="checkB[]" />
?>

that would be javascript

 

the HTML:

<a href="javascript:select_all('checkB', '1')" id="cAll">Check all</a>

 

<script type="text/javascript">
function select_all(name, value) {
for (i = 0; i < forminputs.length; i++) {
	// regex here to check name attribute
	var regex = new RegExp(name, "i");
	if (regex.test(forminputs[i].getAttribute('name'))) {
		if (value == '1') {
			forminputs[i].checked = true;
			document.getElementById('cAll').innerHTML = 'Uncheck all'
			document.getElementById('cAll').href = 'javascript:select_all(\'messageA\', \'0\')';
		} else {
			forminputs[i].checked = false;
			document.getElementById('cAll').innerHTML = 'Check all'
			document.getElementById('cAll').href = 'javascript:select_all(\'messageA\', \'1\')';
		}
	}
}
}
</script>

thanks for the code, but it doesn't do anything ,have i got it right?

<script type="text/javascript">
function select_all(name, value) {
for (i = 0; i < forminputs.length; i++) {
	// regex here to check name attribute
	var regex = new RegExp(name, "i");
	if (regex.test(forminputs[i].getAttribute('name'))) {
		if (value == '1') {
			forminputs[i].checked = true;
			document.getElementById('cAll').innerHTML = 'Uncheck all'
			document.getElementById('cAll').href = 'javascript:select_all(\'messageA\', \'0\')';
		} else {
			forminputs[i].checked = false;
			document.getElementById('cAll').innerHTML = 'Check all'
			document.getElementById('cAll').href = 'javascript:select_all(\'messageA\', \'1\')';
		}
	}
}
}
</script>
<?php
if ($_SESSION['dark_flame'] == true)
{
?>
<table class="admin_cp" cellspacing="0" cellpadding="3"><tr><td>Test</td></tr></table>
<?php
}
$time_stamp = date("d.m.Y");
$date = $time_stamp;
if ($_SESSION['dark_flame'] == true)
{
			if(isset($_POST['delete']))
			{
				foreach($_POST['checkB'] as $checked)
				{
				mysql_query("DELETE FROM darkflame_news WHERE id='{$checked}'") or die("Error:" . mysql_error());
				}
			}

if(isset($_GET['delete_news']))
{
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<table class="news_profile"><tr><th>Delete News</th><td class="delete">Delete</td></tr><tr><td align="left">
<?php
//No ID passed to page, display user list:
if($_SESSION['lang_en'] == true)
{
$query = "SELECT id, name, content, time FROM darkflame_news ORDER BY id DESC LIMIT 5";
}
else
{
$query = "SELECT id, name, content, time FROM darkflame_news_ru ORDER BY id DESC LIMIT 5";
}
$result = mysql_query($query) or die("Error:" . mysql_error());


if (mysql_num_rows($result) > 0) 
{
	while ($row = mysql_fetch_assoc($result)) {
	$content = $row['content'];
	$cont = wordwrap($content, 118, "<br />", true);
	echo "<b>".$row['time']."</b>"; ?> - <a href="?news_id=<?php echo $row['id']; ?>"><?php echo $row['name']; ?></a></td><td align="right"><input type="checkbox" value="<?php echo $row['id']; ?>" name="checkB[]" /></td></tr><tr align="left"><td><?php echo nl2br(stripslashes($cont));; ?>
	<?php
	}
?><tr><td><input type="submit" name="delete" value="Delete Selected"></td><td><a href="javascript:select_all('checkB', '1')" id="cAll">Check all</a></td></tr>
?>

try this whole thing, and replace "messageA" with "checkB"

 

and give your form an id "mailForm"

<form id="mailForm" action="..." method="post">

 

<script type="text/javascript">

var formblock;
var forminputs;
function prepare() {
formblock= document.getElementById('mailForm');
forminputs = formblock.getElementsByTagName('input');
}

function select_all(name, value) {
for (i = 0; i < forminputs.length; i++) {
	// regex here to check name attribute
	var regex = new RegExp(name, "i");
	if (regex.test(forminputs[i].getAttribute('name'))) {
		if (value == '1') {
			forminputs[i].checked = true;
			document.getElementById('cAll').innerHTML = 'Uncheck all'
			document.getElementById('cAll').href = 'javascript:select_all(\'messageA\', \'0\')';
		} else {
			forminputs[i].checked = false;
			document.getElementById('cAll').innerHTML = 'Check all'
			document.getElementById('cAll').href = 'javascript:select_all(\'messageA\', \'1\')';
		}
	}
}
}

if (window.addEventListener) {
window.addEventListener("load", prepare, false);
} else if (window.attachEvent) {
window.attachEvent("onload", prepare)
} else if (document.getElementById) {
window.onload = prepare;
}
</script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.