Solved

Delete Records using Checkboxes

Posted on 2007-03-29
26
226 Views
Last Modified: 2011-10-03
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
0
Comment
Question by:CalmSoul
  • 13
  • 9
  • 3
  • +1
26 Comments
 
LVL 4

Expert Comment

by:mukhtar2t
Comment Utility
what do you mean by deleting records
0
 
LVL 5

Author Comment

by:CalmSoul
Comment Utility
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
0
 
LVL 4

Expert Comment

by:secondv
Comment Utility
0
 
LVL 4

Expert Comment

by:mukhtar2t
Comment Utility
You can do something like this:
<form ...>
inside your table data you can add
<input type = "checkbox" name="check1[]"...>
</form>
0
 
LVL 4

Expert Comment

by:mukhtar2t
Comment Utility
and you can check them by this
foreach ($check1 as $key = $value)
{
delete the corrosponding $key row
}
0
 
LVL 5

Author Comment

by:CalmSoul
Comment Utility
mukhtar2t:

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

Please give more details...

thanks
0
 
LVL 5

Author Comment

by:CalmSoul
Comment Utility
secondv:

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

thanks
0
 
LVL 4

Expert Comment

by:secondv
Comment Utility
Calm, I'm working on an example now :)
0
 
LVL 4

Expert Comment

by:secondv
Comment Utility
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>
0
 
LVL 5

Author Comment

by:CalmSoul
Comment Utility
is this html extension of php?
0
 
LVL 5

Author Comment

by:CalmSoul
Comment Utility
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()); } ?>
0
 
LVL 4

Expert Comment

by:secondv
Comment Utility
save that  with the  .php extension
0
 
LVL 5

Author Comment

by:CalmSoul
Comment Utility
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
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 4

Expert Comment

by:secondv
Comment Utility
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>
0
 
LVL 2

Expert Comment

by:wakemup
Comment Utility
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.
0
 
LVL 5

Author Comment

by:CalmSoul
Comment Utility
secondv:

i posted my code here

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

thanks
0
 
LVL 5

Author Comment

by:CalmSoul
Comment Utility
here is how my database table look

http://www.geopakistani.com/rdb/show.php
0
 
LVL 4

Expert Comment

by:secondv
Comment Utility
0
 
LVL 5

Author Comment

by:CalmSoul
Comment Utility
http://www.geopakistani.com/rdb/records.php

checkall is not working and delete is not working....
0
 
LVL 4

Expert Comment

by:secondv
Comment Utility
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.
0
 
LVL 5

Author Comment

by:CalmSoul
Comment Utility
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

0
 
LVL 4

Expert Comment

by:secondv
Comment Utility
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">
0
 
LVL 5

Author Comment

by:CalmSoul
Comment Utility
both of them doesn;t wory anyother ideas?
0
 
LVL 5

Author Comment

by:CalmSoul
Comment Utility
both of them doesn;t work any  other ideas?

thanks
0
 
LVL 4

Accepted Solution

by:
secondv earned 500 total points
Comment Utility
Strange. I see nothing that would cause that. You could add this after the mysql_query that deletes what you choose:

echo '<script language="JavaScript">window.location=\'records.php\';</script>';


So that it looks like:

      mysql_query("
            DELETE FROM requests
            WHERE Request IN($ids)
      ") or die(mysql_error());
      echo '<script language="JavaScript">window.location=\'records.php\';</script>';
0
 
LVL 5

Author Comment

by:CalmSoul
Comment Utility
yes this one worked
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now