Jump to content

dynamic checkbox for delete system


SkullzY88

Recommended Posts

hey

 

here is the problem am using a javascript for my checkbox1 so that wen it is checked it will check all the other check boxs BUT!

 

in order for the javascript to work i have to name the other checkboxes the fallowing

 

<?PHP
echo '<input type="checkbox" name="chk['.$news['id'].']" />';
?>

bascialy the $news['id'] is the ID of each row in the database

 

everything is fine untile it comes to deleting if i named the checkboxes chk[] it wuld work no problem but because there is a numeric in the middle chk[1] <-[example]

 

it refuse to to delete here is the code for the delete action

 

if(isset($_POST['delete'])) {

foreach($_POST['chk'] as $value) {
$query = mysql_query("DELETE FROM site_news WHERE id='$value'");

echo "<center>News successfully deleted</center>";
echo "<META HTTP-EQUIV=Refresh CONTENT=\"3; URL=./?dir=news&file=delete\">";
}
}

 

cant seem to find the answer ive been searching google-php.net-this forum and others and cant seem to figure this out

 

like i said before it works if the checkboxes where named chk[] but not if it was somthing like chk[1].

 

can anyone help me ?

 

thanks to any replies i appreciate it

Link to comment
https://forums.phpfreaks.com/topic/95037-dynamic-checkbox-for-delete-system/
Share on other sites

hey found it lol

 

must of skipped it on php.net handy tip look carefully  look more than twice :P

 

here is it fixed

 

if(isset($_POST['delete'])) {

while(list($key,$value) = each($_POST['chk'])) { 
$query = mysql_query("DELETE FROM site_news WHERE id='$key'");

echo "<center>News successfully deleted</center>";
echo "<META HTTP-EQUIV=Refresh CONTENT=\"3; URL=./?dir=news&file=delete\">";
}
}

If the checkboxes have the id as the value

<?php
echo '<input type="checkbox" name="chk[]" value="{$news['id']}" />';
?>

 

then you can do all the deletions with a single query

 

<?php
$deletes = join(',', $_POST['chk'] );
$query = mysql_query("DELETE FROM site_news WHERE id IN ($deletes) ");

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.