i want to get the checked checkboxes values to get deleted from the database.can u please help me in this ........................urgent.

Dani AI

Generated

originally asked how to collect checked boxes and delete those rows; posted a working Classic ASP sample and corrected the logical operator issue (using OR rather than AND). That answer is a good starting point, but it misses two important production concerns: robust handling of the Request.Form return value (it may be a single value, a comma-list, or an array) and SQL safety. The snippet below shows a compact, practical approach that addresses both—sanitize numeric IDs server-side, build a safe IN(...) list, and execute inside a transaction.

Function GetSanitizedIdList(fieldName)
    Dim raw, i, parts, v, out
    raw = Request.Form(fieldName)
    If IsEmpty(raw) Or Len(Trim(raw)) = 0 Then
        GetSanitizedIdList = ""
        Exit Function
    End If

    out = ""
    If IsArray(raw) Then
        For i = 0 To UBound(raw)
            v = Trim(raw(i))
            If IsNumeric(v) Then
                If out <> "" Then out = out & ","
                out = out & CInt(v)
            End If
        Next
    Else
        parts = Split(CStr(raw), ",")
        For i = 0 To UBound(parts)
            v = Trim(parts(i))
            If IsNumeric(v) Then
                If out <> "" Then out = out & ","
                out = out & CInt(v)
            End If
        Next
    End If

    GetSanitizedIdList = out
End Function

' Usage (example):
Dim ids, sql
ids = GetSanitizedIdList("chkBox")
If ids <> "" Then
    sql = "DELETE FROM yourTable WHERE id IN (" & ids & ")"
    cn.BeginTrans
    cn.Execute sql
    If Err.Number <> 0 Then cn.RollbackTrans Else cn.CommitTrans
End If

Extra notes: always validate that the current user is authorized to delete, run a SELECT first to preview affected rows, log deletions, and avoid echoing raw DB errors to users. For high-security needs or non-numeric keys use parameterized commands or stored procedures; for Microsoft Jet/Access that often means building validated, type-checked lists as shown.

Recommended Answers

All 2 Replies

Hi there,
have you tried to make this work? ;)
well here the way do do this :
ASP :

<%
'declare variables
Dim connstring, cn, rs
'Connection String
connString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/conn/db1.mdb")
Sub OpenDataBase()
	Set cn = Server.CreateObject("ADODB.CONNECTION")
	cn.open connstring
End Sub
	'Open Database 
		OpenDataBase()
	'Create Recordset
		set rs = Server.CreateObject("ADODB.Recordset")
		sql = "Select * from users"
		rs.Open sql, cn
'If Form Submit this part will Execute		
if Request.Form("Delete") = "Delete" then	
	'Get All Checked checkbox
	checks = Request.Form("chkBox")
	'checkbox array 	
	check_a = split(checks, ",")		
	'SQL	
	str = ""
	str = "Delete from users where "
	'Adding checked checkbox values to Sql string
	for i = 0 to ubound(check_a)
		if i = 0 then
			str = str & "uid = "& check_a(i)
		else
			str = str & " AND uid = "& check_a(i)
		end if
	next
	'Open Database 
	 OpenDataBase()
		'Execute SQL String
		cn.execute(str)	
		Response.Write "Record Deleted!"
%>

HTML :

<%else%>
<!-- Form Not Submited Yet-->
<form action="CheckBox.asp" method="post" name="form1" id="form1">
  <table border="1" cellspacing="2" cellpadding="2">
    <tr>
      <td>Delete</td>
      <td>User Id</td>
      <td>User Name</td>
    </tr>
    <%	
		c=0 
			do while not rs.eof
		c=c+1 
	%>
    <tr>
      <td><input type="checkbox" name="chkBox" value="<%=rs("uid")%>"></td>
      <td><%=rs("uid")%></td>
      <td><%=rs("username")%></td>
    </tr>
    <%
		rs.movenext
		loop
	%>
    <tr>
      <td colspan="3"><input name="Delete" type="submit" value="Delete" /></td>
  </table>
</form>
<%end if%>

I hope this will useful for you and others! :)

Regards,
Rahul Dev Katarey

Hi Again,

if above code not works so just replace

This Part :

for i = 0 to ubound(check_a)
		if i = 0 then
			str = str & "uid = "& check_a(i)
		else
			str = str & " AND uid = "& check_a(i)
		end if
	next

With :

for i = 0 to ubound(check_a)
		if i = 0 then
			str = str & "uid = "& check_a(i)
		else
			str = str & " OR uid = "& check_a(i)
		end if
	next

Thanks
Rahul

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.