Multiple delete using Javascript

This tutorial descibes how to create a delete page using Javascript for multidelete purpose, without having to convert the "coma" into "OR". The SQL command "DELETE * FROM table IN (1,2,etc...)" is used normally in the delete page.

Tutorial enabling multiple delete using SQL command " DELETE * WHERE IN " and Javascript

The aim is to have a "DeletePage.asp" without having the duty to change the "coma" into the clause "OR" :

"Delete * from Table where Number IN (1,2,etc.)" in "Delete * from table where Number=1 OR Number=2 OR..."

The parenthesis are introduced as variables in the "DeletePage.asp"

The way to write the "DefaultPage.asp" (page where you select the records to delete)
has been descibed by Marcellino Bommezijn and Andrew Watson

  • The checkbox name is "checkbox" in the DefaultPage.asp
  • The table's name is "Archive1" in this example

Here is the code of the DeletePage.asp :

<%@LANGUAGE="JAVASCRIPT"%>
<!--#include file="Connections/Gestion.asp" -->

<%
// Script enabling multiple delete of records

// If there is no choice, the page "DeletePage.asp" is loaded again
// and a message box alerts the user
var emptystring=String(Request.Querystring("checkbox")) // The var "emptystring" is used to check the "choice or no choice"
if (emptystring !="undefined") { // If the var "emptystring" contains the user's choice, the datas are deleted

var parenthesisin="("; //var containing the string "("
var parenthesisout=")"; //var containing the string ")"
//Var "parenthesisin" and "parenthesisout" are added before and after the var "String(Request.Querystring("checkbox"))"
if (String(Request.Querystring("checkbox")) != "undefined"){ Command1__VarSuppr =parenthesisin + String(Request.Querystring("checkbox"))+ parenthesisout;}
var Command1 = Server.CreateObject("ADODB.Command");
Command1.ActiveConnection = MM_Gestion_STRING;
// Create a command in "Data Bindings", type="DELETE", SQL="DELETE * FROM Archive1 WHERE Numero IN VarSuppr"
// VarSuppr is a var which value is "Request.Querystring("checkbox")"
Command1.CommandText = "DELETE * FROM Archive1 WHERE Numero IN "+ Command1__VarSuppr.replace(/'/g, "''") + "";
Command1.CommandType = 1;
Command1.CommandTimeout = 0;
Command1.Prepared = true;
Command1.Execute();
Response.Redirect("DefaultPage.asp")
}

else {%>

<HTML>
<HEAD>
<script language="JavaScript">
onLoad=window.alert('You did not select any items to delete !!') // An alert message is showed if no items were selected
window.history.go(-1) // If no items were selected, the navigator goes bak to the "DefaultPage.asp"
</script>
</HEAD>
<BODY>
</BODY>
</HTML>

<%
}%>

Comments

Be the first to write a comment

You must me logged in to write a comment.