Solved

How to select multiple checkboxes

Posted on 2011-02-27
11
337 Views
Last Modified: 2012-06-21
I would like to select/deselect all checkboxes by clicking the checkbox in the table header.
When all checkboxes are selected I would like to submit my form to be processed by my Delete.asp
Would you please help me write the code? I'd really appreciate your help, thank you.
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/DB/Autos.mdb"))

set rs = Server.CreateObject("ADODB.recordset")
rs.Open "SELECT * FROM Auto_Table", conn
%>

<html>
<head>
<title>All Requests</title>
</head>
<body>

<table border="1">
<tr>
<th>Employee Name</th>
<th>Emp.#</th>
<th>Phone Number</th>
<th>Date</th>
<th><input type="checkbox"></th>
</tr>
<%
if not (rs.EOF or rs.BOF) then rs.movefirst
do until rs.EOF
%>
<tr>
<td><%=rs("EmpName")%></td>
<td><%=rs("Emp_Id")%></td>
<td><%=rs("PhoneNo")%></td>
<td><%=rs("adj_svc_dt")%></td>
<td><input type="checkbox" name="Process"></td>
</tr>
<%
rs.MoveNext
loop
rs.Close
conn.Close
%>
</table>

</body>
</html>

Open in new window

0
Comment
Question by:baxtalo
  • 6
  • 4
11 Comments
 

Author Comment

by:baxtalo
Comment Utility
I managed to select all checkboxes by clicking the checkbox in the TH.
I just don't know how I can delete all selected records at the same time.
Would you please have a look at the code below. What do I need to add so that my Delete.asp can process the query?
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/EWR/DB/Autos.mdb"))

set rs = Server.CreateObject("ADODB.recordset")
rs.Open "SELECT * FROM EWRAuto_Table", conn
%>

<html>
<head>
<title>All Requests</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
}
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
}
}
// End -->
</script>

</head>
<body>

<form name=myform action="Delete.asp" method=post>

<table border="1">
<tr>
<th>Employee Name</th>
<th>Emp.#</th>
<th>Phone Number</th>
<th>Date</th>
<th><input type="checkbox" onClick="this.value=check(this.form.ID)"></th>
</tr>
<%
if not (rs.EOF or rs.BOF) then rs.movefirst
do until rs.EOF
%>
<tr>
<td><%=rs("EmpName")%></td>
<td><%=rs("Emp_Id")%></td>
<td><%=rs("PhoneNo")%></td>
<td><%=rs("adj_svc_dt")%></td>
<td><input type="checkbox" name="ID" value="<%=rs("ID") %>"></td>
</tr>
<%
rs.MoveNext
loop
rs.Close
conn.Close
%>
</table>

<input type="Submit" name="Submit" value="Submit" />

</form>


</body>
</html>

Open in new window

0
 
LVL 20

Expert Comment

by:Proculopsis
Comment Utility

Are you after something like this.
0
 

Author Comment

by:baxtalo
Comment Utility
I would like to process all records at the same time. I'm getting now closer, but the version below deletes only one record upon Submit. I would like all checked records deleted at the same time. Please take a look:
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/DB/Autos.mdb"))

set rs = Server.CreateObject("ADODB.recordset")
rs.Open "SELECT * FROM Auto_Table", conn
%>

<html>
<head>
<title>All Requests</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
}
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
}
}
// End -->
</script>

</head>
<body>

<form name=myform action="Delete.asp?ID=<% = rs("ID") %>" method=post>

<table border="1">
<tr>
<th>Employee Name</th>
<th>Emp.#</th>
<th>Phone Number</th>
<th>Date</th>
<th><input type="checkbox" onClick="this.value=check(this.form.ID)"></th>
</tr>
<%
if not (rs.EOF or rs.BOF) then rs.movefirst
do until rs.EOF
%>
<tr>
<td><%=rs("EmpName")%></td>
<td><%=rs("Emp_Id")%></td>
<td><%=rs("PhoneNo")%></td>
<td><%=rs("adj_svc_dt")%></td>
<td><input type="checkbox" name="ID" value="<%=rs("ID") %>"></td>
</tr>
<%
rs.MoveNext
loop
rs.Close
conn.Close
%>
</table>

<input type="Submit" name="Submit" value="Submit" />

</form>

</body>
</html>

Open in new window

0
 
LVL 9

Expert Comment

by:rg20
Comment Utility
To delete multiple records, you will need to know which checkboxes are to which records.

What I do is give the checkbox a name like this
checkbox name="ckbx_"&ID

After the user checkes everything, you just cycle through the request form checkboxes for the name starting with ckbx_.
Then parse the name and you know what record to delete.

0
 

Author Comment

by:baxtalo
Comment Utility
I'm struggling with this since this morning. I would rather like to update the checked records.
The following fields need to be updated: AutoStatus, Pipa, ApprovedBy
When I submit I get the following error:

Microsoft JET Database Engine error '80040e14'
Syntax error (comma) in query expression 'ID=3, 4, 5, 6, 7, 8, 9, 10, 11'.

Please have a look at my pages (All_.asp and Process.asp); would you know why it won't update my fields?
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/EWR/DB/Autos.mdb"))

set rs = Server.CreateObject("ADODB.recordset")
rs.Open "SELECT * FROM EWRAuto_Table", conn
%>

<html>
<head>
<title>All Requests</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function checkLikeMe($) {
    var x = document.getElementsByName('ID');
    for (var y = 0; y < x.length; y++)
        x[y].checked = $;
}

// End -->
</script>

</head>
<body>

<form name="editor" method="post" action="Process.asp">

<table border="1">
<tr>
<th>Employee Name</th>
<th>Emp.#</th>
<th>Auto Status</th>
<th><input type="checkbox" onClick="checkLikeMe(this.checked)"></th>
</tr>
<%
if not (rs.EOF or rs.BOF) then rs.movefirst
do until rs.EOF
%>
<tr>
<td><%=rs("EmpName")%></td>
<td><%=rs("Emp_Id")%></td>
<td><%=rs("AutoStatus")%></td>
<td><input type="checkbox" name="ID" value="<%=rs("ID") %>"></td>
</tr>
<%
rs.MoveNext
loop
rs.Close
conn.Close
%>
</table>


<br /><br />
<select name="AutoStatus">
<option value="Pending">Select Status</option>
<option value="Disapproved">Disapproved</option>
<option value="Approved">Approved</option>
</select>

<input type="hidden" name="Pipa" value="&#252">
<input name="ApprovedBy" type="hidden" id="ApprovedBy" value="<%=Request.ServerVariables("LOGON_USER")%>" />
<input type="Submit" name="Submit" value="Update" />
</center>
</form>

</body>
</html>


AND THIS IS the Process.asp:

<%
Set objConn = CreateObject("ADODB.Connection")
objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath ("/DB/Autos.mdb") & ";"
objConn.Open

myId = Request.form("ID")

myAutoStatus = request.Form("AutoStatus")
myPipa = request.Form("Pipa")
myApprovedBy = request.Form("ApprovedBy")


StrSQL = "Update Auto_Table SET AutoStatus= '" & myAutoStatus & "', Pipa= '" & myPipa & "', ApprovedBy= '" & myApprovedBy & "' Where ID="&myId&""
objConn.Execute StrSQL

'Redirect to the UPDATED page
Response.Redirect "All_.asp"
%>

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 9

Accepted Solution

by:
rg20 earned 500 total points
Comment Utility
try
StrSQL = "Update Auto_Table SET AutoStatus= '" & myAutoStatus & "', Pipa= '" & myPipa & "', ApprovedBy= '" & myApprovedBy & "' Where ID in ("&myId&"}"

0
 

Author Comment

by:baxtalo
Comment Utility
I tried and now I get this error message:

Microsoft JET Database Engine error '80040e14'
Syntax error in query expression 'ID in (3, 4, 5, 6, 7, 8, 9, 10, 11}'.
0
 
LVL 9

Expert Comment

by:rg20
Comment Utility
If your ID's are strings, then you need to do a replace

Where ID in ("&replace(myId,",", """,""" &"}"
 before executing your SQL, can you do a response.write to see the entire query?
0
 

Author Comment

by:baxtalo
Comment Utility
Where do I have to put the response.write? When I used Where ID in ("&replace(myId,",", """,""" &"}"
I got this error:
Microsoft VBScript compilation error '800a03ee'

Expected ')'

/EWR/Autos/Process.asp, line 14

StrSQL = "Update Auto_Table SET AutoStatus= '" & myAutoStatus & "', Pipa= '" & myPipa & "', ApprovedBy= '" & myApprovedBy & "' Where ID in ("&replace(myId,",", """,""" &"}"

0
 
LVL 9

Expert Comment

by:rg20
Comment Utility
I forgot a closing bracket for the replace

StrSQL = "Update Auto_Table SET AutoStatus= '" & myAutoStatus & "', Pipa= '" & myPipa & "', ApprovedBy= '" & myApprovedBy & "' Where ID in ("&replace(myId,",", """,""") &"}"

0
 

Author Closing Comment

by:baxtalo
Comment Utility
Thank you very much, your solution is perfect. I just made a spelling mistake; that's why it didn't work when I tried it for the first time. Now it's working, thanks again...
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now