Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to select multiple checkboxes

Posted on 2011-02-27
11
341 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
ID: 34992193
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
ID: 34992234

Are you after something like this.
0
 

Author Comment

by:baxtalo
ID: 34992265
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 9

Expert Comment

by:rg20
ID: 34993621
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
ID: 34993763
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
 
LVL 9

Accepted Solution

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

0
 

Author Comment

by:baxtalo
ID: 34993800
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
ID: 34993826
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
ID: 34993876
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
ID: 34994042
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
ID: 34994043
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

808 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