Link to home
Start Free TrialLog in
Avatar of CalmSoul
CalmSoulFlag for United States of America

asked on

Delete Records using Checkboxes

Hello,
Can somebody provide me the link of the tutorial ? where they is an example of "Delete Records using Checkboxes " ?

Code examples will be good too

thanks
Avatar of mukhtar2t
mukhtar2t

what do you mean by deleting records
Avatar of CalmSoul

ASKER

I am using select and getting data from the database... displaying in the html table

I want checkboxes to appear infront of each row and when it is check and user presses the delete button ... I want that row to be delete from the table

thanks
You can do something like this:
<form ...>
inside your table data you can add
<input type = "checkbox" name="check1[]"...>
</form>
and you can check them by this
foreach ($check1 as $key = $value)
{
delete the corrosponding $key row
}
mukhtar2t:

I need more solid example this question worth 500 pts...

Please give more details...

thanks
secondv:

yes this is what I am looking for but... code example please

thanks
Calm, I'm working on an example now :)
Here you go:

<!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>
<title>Remove records</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-us" />
<script type="text/javascript">
<!--
function checkAll(ref)
{
      var chkAll = document.getElementById('checkAll');
      var checks = document.getElementsByName('del[]');
      var removeButton = document.getElementById('removeChecked');
      var boxLength = checks.length;
      var allChecked = false;
      var totalChecked = 0;

      if(ref == 1)
      {
            if (chkAll.checked == true)
            {
                  for(i = 0; i < boxLength; i++)
                  {
                        checks[i].checked = true;
                  }
            }
            else
            {
                  for(i = 0; i < boxLength; i++)
                  {
                        checks[i].checked = false;
                  }
            }
      }
      else
      {
            for(i = 0; i < boxLength; i++)
            {
                  if (checks[i].checked == true)
                  {
                        allChecked = true;
                        continue;
                  }
                  else
                  {
                        allChecked = false;
                        break;
                  }
            }

            if (allChecked == true)
            {
                  chkAll.checked = true;
            }
            else
            {
                  chkAll.checked = false;
            }
      }

      for(j = 0; j < boxLength; j++)
      {
            if (checks[j].checked == true)
            {
                  totalChecked++;
            }
      }
      removeButton.value = 'Remove [' + totalChecked + '] Selected';
}
// -->
</script>
</head>

<body>
<form action="#" method="post" onsubmit="return confirm('Are you sure you want to delete the selected items?');">
<table>
<caption>Remove records</caption>
<thead>
<tr>
      <th><input type="checkbox" onclick="checkAllFields(1);" id="checkAll" /></th>
      <th>Name</th>
      <th>Email</th>
      <th>Date Registered</th>
</tr>
</thead>
<tbody>
<?php

// connect to database here
//
$users = mysql_query("
      SELECT userid, name, email, dateregistered
      FROM users
");

while ($user = mysql_fetch_assoc($users))
{
?>
<tr>
      <td><input type="checkbox" value="<?php echo $user['userid']; ?>" name="del[]" onclick="checkAllFields(2);" /></td>
      <td><?php echo $user['name']; ?></td>
      <td><?php echo $user['email']; ?></td>
      <td><?php echo date('m/d/Y g:i A', $user['dateregistered']); ?></td>
</tr>
<?php
}
?>
<tr>
      <td colspan="4"><input type="submit" name="remove" value="Remove [0] Selected" id="removeChecked" /></td>
</tr>
</tbody>
</table>
</form>

<?php

if (!empty($_POST['remove']))
{
      $rcount = count($_POST['del']);

      for ($i = 0; $i < $rcount; $i++)
      {
            $ids .= (trim($_POST['del'][$i]) != '') ? trim($_POST['del'][$i]) . ',' : '';
      }
      $ids = rtrim($ids, ',');

      mysql_query("
            DELETE FROM users
            WHERE userid IN($deleteIds)
      ") or die(mysql_error());
}

?>

</body>
</html>
is this html extension of php?
secondv:

This example is too complicated....

I am getting lots of errors....

here is what I get

" name="del[]" onclick="checkAllFields(2);" />    
 

<?php if (!empty($_POST['remove'])) { $rcount = count($_POST['del']); for ($i = 0; $i < $rcount; $i++) { $ids .= (trim($_POST['del'][$i]) != '') ? trim($_POST['del'][$i]) . ',' : ''; } $ids = rtrim($ids, ','); mysql_query(" DELETE FROM requests WHERE Request IN($deleteIds) ") or die(mysql_error()); } ?>
save that  with the  .php extension
here is the error with php ext

Parse error: syntax error, unexpected ')', expecting ',' or ';' in /home/xx/public_html/xx/records.php on line 109
Perhaps show me your file, by pasting the code you have here: http://paste.biz

I did not get any errors. http://scriptgod.com/records.php

And, I've revised the JavaScript a bit:


<!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>
<title>Remove records</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-us" />
<script type="text/javascript">
function checkAll()
{
    var delArray = document.getElementsByName('del[]');

    if(delArray)
    {
        for(var i = 0; i < delArray.length; i++)
        {
            if(delArray[i].checked == false)
            {
                delArray[i].checked = true;
            }
        }
    }
}
</script>

</head>

<body>
<form action="" method="post" onsubmit="return confirm('Are you sure you want to delete the selected items?');">
<table>
<caption>Remove records</caption>
<thead>
<tr>
      <th><a href="javascript:checkAll();">All</a></th>
      <th>Name</th>
      <th>Email</th>
      <th>Date Registered</th>
</tr>
</thead>
<tbody>
<?php

$link =& mysql_connect('localhost', '', '');
mysql_select_db('', $link);

$users = mysql_query("
      SELECT userid, name, email, dateregistered
      FROM users
");

while ($user = mysql_fetch_assoc($users))
{
?>
<tr>
      <td><input type="checkbox" value="<?php echo $user['userid']; ?>" name="del[]" /></td>
      <td><?php echo $user['name']; ?></td>
      <td><?php echo $user['email']; ?></td>
      <td><?php echo date('m/d/Y g:i A', $user['dateregistered']); ?></td>
</tr>
<?php
}
?>
<tr>
      <td colspan="4"><input type="submit" name="remove" value="Remove [0] Selected" id="removeChecked" /></td>
</tr>
</tbody>
</table>
</form>

<?php

if (!empty($_POST['remove']))
{
      $rcount = count($_POST['del']);

      for ($i = 0; $i < $rcount; $i++)
      {
            $ids .= (trim($_POST['del'][$i]) != '') ? trim($_POST['del'][$i]) . ',' : '';
      }
      $ids = rtrim($ids, ',');
 
      mysql_query("
            DELETE FROM users
            WHERE userid IN($ids)
      ") or die(mysql_error());
}

?>

</body>
</html>
The key thing is that you can put [] at the end of the checkbox name attribute (i.e.

<input type="checkbox" value="<recordid>" name="chk[]" />

and you will get an array of chk[] items in your script with the values of the checked boxes in it. Then use this to delete the record from the database.
secondv:

i posted my code here

http://paste.biz/paste-1398.html

thanks
here is how my database table look

http://www.geopakistani.com/rdb/show.php
http://www.geopakistani.com/rdb/records.php

checkall is not working and delete is not working....
http://scriptgod.com/records.txt

New javascript and added a function to quote a string :) I was assuming you'd be passing id's (numbers) to the delete, not strings, but that is taken care of now.
Secondv:

Thanks for your help.. but one issue post back is not happening ... I have to refresh the page then data goes away.... is there a way to resolve this?

http://www.geopakistani.com/rdb/records.php

change:

<form action="" method="post" onsubmit="return confirm('Are you sure you want to delete the selected items?');">


To either:
 <form action="records.php" method="post" onsubmit="return confirm('Are you sure you want to delete the selected items?');">


and if that doesn't work, try:

<form action="records.php" method="post">
both of them doesn;t wory anyother ideas?
both of them doesn;t work any  other ideas?

thanks
ASKER CERTIFIED SOLUTION
Avatar of secondv
secondv
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yes this one worked