Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delete Records using Checkboxes

Posted on 2007-03-29
26
Medium Priority
?
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 9
  • 3
  • +1
26 Comments
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18817416
what do you mean by deleting records
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 18817440
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
ID: 18817447
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18817466
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
ID: 18817478
and you can check them by this
foreach ($check1 as $key = $value)
{
delete the corrosponding $key row
}
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 18817597
mukhtar2t:

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

Please give more details...

thanks
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 18817602
secondv:

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

thanks
0
 
LVL 4

Expert Comment

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

Expert Comment

by:secondv
ID: 18817752
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
ID: 18817843
is this html extension of php?
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 18818323
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
ID: 18818382
save that  with the  .php extension
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 18818407
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
 
LVL 4

Expert Comment

by:secondv
ID: 18818703
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
ID: 18818874
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
ID: 18818979
secondv:

i posted my code here

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

thanks
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 18818982
here is how my database table look

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

Expert Comment

by:secondv
ID: 18819060
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 18819128
http://www.geopakistani.com/rdb/records.php

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

Expert Comment

by:secondv
ID: 18819287
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
ID: 18820472
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
ID: 18820535
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
ID: 18820556
both of them doesn;t wory anyother ideas?
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 18820595
both of them doesn;t work any  other ideas?

thanks
0
 
LVL 4

Accepted Solution

by:
secondv earned 2000 total points
ID: 18820599
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
ID: 18820616
yes this one worked
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

670 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