
Last Time I post “Deleting multiple records using checkbox”. One of my friend request me to add some features like yahoo mail, gmail where we can select all or unselect all records. WOW! it was really helpful for everyone. So I am here going to show you Delete Multiple Records Using Select All and Unselect Features. I have simply added a javascript code in my previous post which you can see here. All of the Features are same as my previous code and I am using the new codes which I added on my last one.
Now Create your database and table as your required. Write your connection string and finish all of the work. Which you can see in my previous post.
Ok Now add this javascript before your </head> tag.
<script>
function selectAllCheckBoxes(FormName, FieldName, CheckValue)
{
if(!document.forms[FormName])
return;
var objCheckBoxes = document.forms[FormName].elements[FieldName];
if(!objCheckBoxes)
return;
var countCheckBoxes = objCheckBoxes.length;
if(!countCheckBoxes)
objCheckBoxes.checked = CheckValue;
else
// set the check value for all check boxes
for(var i = 0; i < countCheckBoxes; i++)
objCheckBoxes[i].checked = CheckValue;
}
</script>
And Now add this code before </form> tag. Which add two buttons in your code.
<input type="button" onclick="selectAllCheckBoxes('form1', 'checkbox[]', true);" value="Select All">
<input type="button" onclick="selectAllCheckBoxes('form1', 'checkbox[]', false);" value="Clear All">
You have done.
Get Complete Code Here:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Delete Multiple Records</title>
<script>
function selectAllCheckBoxes(FormName, FieldName, CheckValue)
{
if(!document.forms[FormName])
return;
var objCheckBoxes = document.forms[FormName].elements[FieldName];
if(!objCheckBoxes)
return;
var countCheckBoxes = objCheckBoxes.length;
if(!countCheckBoxes)
objCheckBoxes.checked = CheckValue;
else
// set the check value for all check boxes
for(var i = 0; i < countCheckBoxes; i++)
objCheckBoxes[i].checked = CheckValue;
}
</script>
</head>
<body>
<?php
$dbhost="localhost";
$dbuser="root";
$dbpass="";
$dbname="test";
mysql_connect("$dbhost","$dbuser","$dbpass") or die('Could not connect');
mysql_select_db($dbname);
?>
<form name="form1" method="post" action="">
<table width="409" border="0" cellpadding="2" cellspacing="0" bordercolor="#999999">
<tr bgcolor="#CCCCCC">
<td width="52"><div align="center">Select</div></td>
<td width="98"><div align="center">Name</div></td>
<td width="142"><div align="center">Address</div></td>
<td width="101"><div align="center">Contact No</div></td>
</tr>
<?php
$query="SELECT * FROM tbluser";
$result=mysql_query($query);
$sno=1;
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
?>
<tr>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['userid']; ?>"></td>
<td><? echo $row['name'];?></td>
<td><? echo $row['address'];?></td>
<td><? echo $row['contactno'];?></td>
</tr>
<?
$sno=$sno+1;
}
?>
<tr>
<td> </td>
<td colspan="3">
<div align="right">
<input name="delete" type="submit" id="delete" value="Delete Records">
<input type="button" onclick="selectAllCheckBoxes('form1', 'checkbox[]', true);" value="Select All">
<input type="button" onclick="selectAllCheckBoxes('form1', 'checkbox[]', false);" value="Clear All">
</div></td>
</tr>
</table>
<?
if($_POST['delete']){
//print_r($_POST);
$checkbox=$_POST['checkbox'];
//exit;
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM tbluser WHERE userid='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=deletemultiplerecords.php\">";
}
}
?>
</form>
</body>
</html>
Source: www.jagadishwor.com.np

RSS Feeds
Feed Comment 




Thank you SO much, I had this script some time ago, but it stopped working for some reason, but now it works like an charm and I can even select all entries in the database without any problems, so once again Thanks man !
You truely saved my day…