Delete multiple records using checkboxes in Coldfusion
This is the example to delete, active and inactive multiple rows from mysql using javascript and Coldfusion. We can checked all checkboxes and validate if no any checkbox checked.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JavaScript Document | |
//Check All Check Box | |
function CheckAll() | |
{ | |
var i; | |
for(i=0;i<document.frmmain.elements.length; i++) | |
{ | |
var elm = document.frmmain.elements[i]; | |
if(elm.type == "checkbox" && elm.name != "chkCheckAll" ) | |
{ | |
elm.checked =document.frmmain.chkCheckAll.checked; | |
} | |
} | |
} | |
function Check() | |
{ | |
var i; | |
var flag; | |
flag=1; | |
for(i=0;i<document.frmmain.elements.length; i++) | |
{ | |
var elm = document.frmmain.elements[i]; | |
if(elm.type == "checkbox" && elm.name != "chkCheckAll" ) | |
{ | |
if(elm.checked==false) | |
{ | |
flag=0; | |
} | |
} | |
} | |
if(flag==1) | |
{ | |
document.frmmain.chkCheckAll.checked=true; | |
} | |
else | |
{ | |
document.frmmain.chkCheckAll.checked=false; | |
} | |
} | |
function checkBest() | |
{ | |
if( (parseInt(document.frmmain.BestCount.value) + parseInt(numChecked())) > 3 ) | |
{ | |
alert("Only three properties can be selected as best proposal at a time. Please check your selection"); | |
return false; | |
} | |
else | |
{ | |
frmmain.submit(); | |
} | |
} | |
//Confirm message to make Delete | |
function checkDelete() | |
{ | |
rtn=confirm("Are you sure you want to Delete this record(s) ?"); | |
if(rtn==false) | |
{ | |
return false; | |
} | |
else | |
{ | |
document.frmmain.actionVal.value="Delete"; | |
//return true; | |
document.frmmain.submit(); | |
} | |
} | |
//Confirm message to make Active | |
function checkApproval() | |
{ | |
rtn=confirm("Are you sure you want to Active this record(s) ?"); | |
if(rtn==false) | |
{ | |
return false; | |
} | |
else | |
{ | |
document.frmmain.actionVal.value="Active"; | |
//return true; | |
document.frmmain.submit(); | |
} | |
} | |
//Confirm message to make InActive | |
function checkReject() | |
{ | |
rtn=confirm("Are you sure you want to Inactive this record(s) ?"); | |
if(rtn==false) | |
{ | |
return false; | |
} | |
else | |
{ | |
document.frmmain.actionVal.value="Inactive"; | |
//return true; | |
document.frmmain.submit(); | |
} | |
} | |
function numChecked() | |
{ | |
j=0; | |
for(i=0;i<document.frmmain.length;i++) | |
{ | |
e=document.frmmain.elements[i]; | |
if (e.type=='checkbox' && e.name != 'chkCheckAll' && e.checked) | |
j++; | |
} | |
return j; | |
} | |
function slct1st() | |
{ | |
j=0; | |
for(i=0;i< document.frmmain.length;i++) | |
{ | |
e=document.frmmain.elements[i]; | |
if (e.type=='checkbox' && e.name != 'chkCheckAll' && e.checked) | |
if(j==1) | |
e.checked=false; | |
else | |
j=1; | |
} | |
return j; | |
} | |
//Submit function according click | |
function Subm(first,dosub,opt) | |
{ | |
for(i=0;i<document.frmmain.length;i++) | |
{ | |
e=document.frmmain.elements[i]; | |
if (e.name == 'chkCheckAll') | |
e.checked=false; | |
} | |
num = ((first) ? slct1st() : numChecked()); | |
if (num>0) | |
{ | |
if(opt==1) | |
return checkDelete(); | |
else if(opt==2) | |
return checkReject(); | |
else if(opt==3) | |
return checkApproval(); | |
else if(opt==4) | |
return checkBest(); | |
} | |
else | |
{ | |
alert("Please select at least one record."); | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfoutput> | |
<script src="checkboxs.js" type="text/javascript"></script> | |
<form method="post" name="frmmain" id="frmmain" action="updaterecords.cfm" class="nice"> | |
<input type="hidden" name="actionVal" value="" /> | |
<div style="width:150px; float:left;"><h2>Student List</h2></div> | |
<div style="width:330px; float:left;" align="right"> | |
<a href="javascript: return false;" style="text-decoration:none"> | |
<button class="styled" type="button" >Add Student</button> | |
</a> | |
<button class="styled" type="button" onclick="return Subm(0,0,1);">Delete </button> | |
<button class="styled" type="button" onclick="return Subm(0,0,3);">Active </button> | |
<button class="styled" type="button" onclick="return Subm(0,0,2);">Inactive </button> | |
</div> | |
<div style="clear:both;"></div> | |
<cfquery name="GetStudents" datasource="TestDB"> | |
select * from student where deleted=0 | |
</cfquery> | |
<table width="25%" border="0" cellpadding="0" cellspacing="0" id="data"> | |
<tr> | |
<th width="5%" align="center" scope="col" class="nosort"> | |
<input name="chkCheckAll" type="checkbox" value="checkbox" onclick="CheckAll();" /> | |
</th> | |
<th width="25%" scope="col" align="left">Student Name</th> | |
<th width="16%" scope="col" align="left">Status</th> | |
</tr> | |
<cfif GetStudents.RecordCount NEQ 0> | |
<cfloop query="GetStudents"> | |
<tr> | |
<td class="odd" width="5%" align="center"><input type="checkbox" value="#idStudent#" name="CheckBoxIDs" <!---name="check_#idStudent#"---> id="check_#idStudent#" /></td> | |
<td class="odd" width="55%">#first_name# #last_name#</td> | |
<td class="odd" width="20%"><cfif Active EQ 0>InActive<cfelse>Active</cfif></td> | |
</tr> | |
</cfloop> | |
<cfelse> | |
<tr> | |
<td class="odd" width="100%" colspan="4" align="center">No Records Found</td> | |
</tr> | |
</cfif> | |
</table> | |
</form> | |
</cfoutput> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- ***************Starts : Multiple Active Reocrds************************ ---> | |
<cfif actionVal EQ "Active"> | |
<cfquery name="GetStudents" datasource="TestDB"> | |
update student set active= 1 where idStudent in (#CheckBoxIDs#) | |
</cfquery> | |
</cfif> | |
<!---***************Ends : Multiple Active Reocrds************************---> | |
<!---***************Starts : Multiple Inactive Reocrds************************---> | |
<cfif actionVal EQ "Inactive"> | |
<cfquery name="GetStudents" datasource="TestDB"> | |
update student set active= 0 where idStudent in (#CheckBoxIDs#) | |
</cfquery> | |
</cfif> | |
<!---***************Ends : Multiple Inactive Reocrds************************---> | |
<!---***************Starts : Multiple Delete Reocrds************************---> | |
<cfif actionVal EQ "Delete"> | |
<cfquery name="GetStudents" datasource="TestDB"> | |
update student set deleted= 1 where idStudent in (#CheckBoxIDs#) | |
</cfquery> | |
</cfif> | |
<!---***************Ends : Multiple Delete Reocrds************************---> | |
<cflocation url="studentlist.cfm" addtoken="no"> |
----------------------------------------------------------------------------------------------------------
If you like this or this is useful for you. Please comment us.
Thank You
Comments
Post a Comment