CalmSoul
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
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
what do you mean by deleting records
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
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
Something like the following maybe?
http://www.dustindiaz.com/check-one-check-all-javascript/
http://www.dustindiaz.com/basement/checkAll.html
http://www.dustindiaz.com/check-one-check-all-javascript/
http://www.dustindiaz.com/basement/checkAll.html
You can do something like this:
<form ...>
inside your table data you can add
<input type = "checkbox" name="check1[]"...>
</form>
<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
}
foreach ($check1 as $key = $value)
{
delete the corrosponding $key row
}
ASKER
mukhtar2t:
I need more solid example this question worth 500 pts...
Please give more details...
thanks
I need more solid example this question worth 500 pts...
Please give more details...
thanks
ASKER
secondv:
yes this is what I am looking for but... code example please
thanks
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-Langua ge" content="en-us" />
<script type="text/javascript">
<!--
function checkAll(ref)
{
var chkAll = document.getElementById('c heckAll');
var checks = document.getElementsByName ('del[]');
var removeButton = document.getElementById('r emoveCheck ed');
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>
<!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-Langua
<script type="text/javascript">
<!--
function checkAll(ref)
{
var chkAll = document.getElementById('c
var checks = document.getElementsByName
var removeButton = document.getElementById('r
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)
<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><?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>
ASKER
is this html extension of php?
ASKER
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()); } ?>
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']))
save that with the .php extension
ASKER
here is the error with php ext
Parse error: syntax error, unexpected ')', expecting ',' or ';' in /home/xx/public_html/xx/re cords.php on line 109
Parse error: syntax error, unexpected ')', expecting ',' or ';' in /home/xx/public_html/xx/re
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-Langua ge" 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>
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-Langua
<script type="text/javascript">
function checkAll()
{
var delArray = document.getElementsByName
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(
<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.
<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.
ASKER
ASKER
Try this http://scriptgod.com/records.txt
ASKER
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.
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.
ASKER
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
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">
<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">
ASKER
both of them doesn;t wory anyother ideas?
ASKER
both of them doesn;t work any other ideas?
thanks
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yes this one worked