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
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\">";
}
}

Link to comment
Share on other sites

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) ");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.