?
Solved

How to select multiple checkboxes

Posted on 2011-02-27
11
Medium Priority
?
344 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
[X]
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
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

752 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