This tutorial, adapted from Chapter 15 of The Joy of Dreamweaver MX, explains how to enable batch deletes via check boxes in three simple steps.
The Joy of Dreamweaver MX: Recipes for Data-Driven Web Sites is a web developer's cookbook filled with dozens of "secret" recipes that DMXers can really sink their teeth into.
For more information, visit the book's companion web site, or read a sample chapter - "Uploads Made Simple" - on UDzone.COM.
Step 3: Revise the SQL DELETE Statement |
|
To complete the batch delete page, switch to code view and locate the DELETE
statement:
MM_editQuery = "delete from " & MM_editTable &
" where " & MM_editColumn & " = " & MM_recordId
Change it to the following (the new code is shaded):
MM_editQuery = "delete from " & MM_editTable &
" where " & MM_editColumn & " IN
(" & MM_recordId & ")"
In Structured Query Language, the IN operator accepts a subquery or comma-delimited
list as an argument. Since the value of Request.Form("MM_recordId")
is a comma-delimited list, we can use the IN operator to perform batch
deletes on the selected table (MM_editTable).
Here is what the code would look like at run-time:
MM_editQuery = "delete from Links where LinkID IN (3, 4,
5, 6)"
That's it! Save your work and press F12 to preview the
page in a browser. Select two or more check boxes and click Delete to submit the form
(make sure to back up your database so you can restore it later).
The batch delete page posts to itself, and the results are displayed immediately.
What's nice about this approach is that you can still access and edit the Delete
Record SB from the Server Behaviors panel, and the batch-delete modification
remains intact.
JavaScript Validation
If you want to validate the form, you can insert the following JavaScript into
the <head> of the document:
<SCRIPT Language="JavaScript" type="text/javascript">
<!--
function Form_Validator(theForm){
// require that at least one check box is checked
var checkSelected = false;
for (i = 0; i < theForm.MM_recordId.length; i++){
if (theForm.MM_recordId[i].checked)
checkSelected = true;
}
if (!checkSelected){
alert("Please select at least one of the check boxes.");
return (false);
}
return(true);
}
//-->
</SCRIPT>
To call the function, add the following code to the <form>
tag:
onSubmit="return Form_Validator(this)"
This ensures that at least one check box is checked before the form is submitted.
Deleting Associated Files
To delete files associated with your database records, you can write the code
yourself (see "Deleting Files Using FSO" in Chapter 15 of The
Joy of Dreamweaver MX), or use George Petrov's Pure
ASP Upload Add On Pack. Since George's extension now supports batch deletes
as well, we'll use it in the following example.
The Pure ASP Upload Add On Pack actually consists of four extensions for deleting,
renaming and mailing uploaded files. For this example, I selected UDzone | Delete
File Before Record from the Server Behaviors panel.
NOTE: If you try to apply this extension without applying the standard
Delete Record SB first, you will be prompted to do so.
Select the database column that contains the filename, and click Browse to
select the folder it resides in (I selected LinkURL
because the Links table doesn't actually contain
a filename column):
Click OK to apply the server behavior. A message box appears, informing you
that a file named incPUAddOn.asp has been copied into the ScriptLibrary folder
in the root of your web site. Click OK. If you switch to Code View, you'll notice
that the following code has been inserted before the SQL DELETE statement:
<%
' *** Delete File Before Delete Record 1.5.0
If (CStr(Request("MM_delete")) <> "" And CStr(Request("MM_recordId"))
<> "") Then
Dim DF_filesStr, DF_path
DF_filesStr = "LinkURL"
DF_path = "../images"
DeleteFileBeforeRecord
DF_filesStr,DF_path,MM_editConnection,MM_editTable,MM_editColumn,MM_recordId
end if
%>
This calls a sub procedure in incPUAddOn.asp that does all the dirty work for
you. If this were an update page, you would apply Delete File Before Update
to delete the associated file before uploading its replacement. With a little
effort, you can even implement batch updates and batch deletes on the
same page (I'll save that topic for a future tutorial). For more on batch updates,
see Chapter 16 of The
Joy of Dreamweaver MX.
Download
Sample Code