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
LVL 5
CalmSoulAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mukhtar2tCommented:
what do you mean by deleting records
0
CalmSoulAuthor Commented:
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
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

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

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

Please give more details...

thanks
0
CalmSoulAuthor Commented:
secondv:

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

thanks
0
secondvCommented:
Calm, I'm working on an example now :)
0
secondvCommented:
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
CalmSoulAuthor Commented:
is this html extension of php?
0
CalmSoulAuthor Commented:
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
secondvCommented:
save that  with the  .php extension
0
CalmSoulAuthor Commented:
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
secondvCommented:
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
wakemupCommented:
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
CalmSoulAuthor Commented:
secondv:

i posted my code here

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

thanks
0
CalmSoulAuthor Commented:
here is how my database table look

http://www.geopakistani.com/rdb/show.php
0
secondvCommented:
0
CalmSoulAuthor Commented:
http://www.geopakistani.com/rdb/records.php

checkall is not working and delete is not working....
0
secondvCommented:
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
CalmSoulAuthor Commented:
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
secondvCommented:
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
CalmSoulAuthor Commented:
both of them doesn;t wory anyother ideas?
0
CalmSoulAuthor Commented:
both of them doesn;t work any  other ideas?

thanks
0
secondvCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CalmSoulAuthor Commented:
yes this one worked
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.