View Full Version : search string with checkboxes

05-04-2006, 12:40 AM
Hi, I'm totally new to asp and need help with checkboxes. When a user searches on our site and there are multiple items, I'd like them to be able to click on the checkboxes and add multiple items to their cart, rather than have to add each item individually. I have to pass the checked items to the shopping cart.

Here's my code so far... I know it's not much, but I have no clue where to go from here!

Next to each item is a checkbox:

<form name="checkbox" method="post" action="check_out.asp?sku_tag=<%=sku%>">
<input type="checkbox" name="checkbox<%=sku%>" value="checkbox<%=sku%>">

Then, down below is a submit button:
<input type="image" src="gfx/checked_addtocart1.jpg" name="Action" value="submit">

I assume I need some kind of "if checked" statement. Any help is MUCH appreciated!

FWIW, this is my work-in-progress page:
(I'm responsible for some of the HTML and none of the ASP on this page.)

05-04-2006, 03:16 PM
You need to give your checkboxes the same name so that if multiple are selected it creates a comma delimited string.

You can then take this string and either split it to create an array or if using SQL you can use the IN clause to query the database*.

* Will only work for numerical values.
If you're quering text then you have to split the text into an array then either loop through each or construct a string.

If Len(Request.Form) > 0 Then
sku = Request.Form("sku")
If Not IsEmpty(sku) Then
sku = Split(sku, ",")
limit = Ubound(sku)
For x=0 To limit
clause = clause & "'" & sku(x) & "',"
clause = Left(clause, Len(clause)-1)

Response.Write "SQL = ""SELECT * FROM table WHERE sku IN (" & clause & ")"""
End If
End If
<form name="form1" method="post" action="">
<input name="SKU" type="checkbox" id="SKU" value="AAA-001">
<input name="SKU" type="checkbox" id="SKU" value="AAA-002">
<input name="SKU" type="checkbox" id="SKU" value="BBB-001">
<input type="submit" name="Submit" value="Submit">

05-04-2006, 11:37 PM
Thank you! Our skus have some text, i.e. AUM337. Does that mean I need to split it into an array?

With that code, it is adding the first item checked into the cart; I suppose I need to add something to the shopping cart page to accept a string?

05-05-2006, 02:38 PM
It would depend on how the cart works.

05-10-2006, 02:18 PM
Hello there,

I have the problem w/ the checkboxes searching too...

here is the html and asp code

<form action="result.asp" method="get">
<input type="checkbox" name="course" value="cos101"> cos101 <br>
<input type="checkbox" name="course" value="cos102"> cos102 <br>
<input type="checkbox" name="course" value="cos103"> cos103 <br>
<input type="checkbox" name="course" value="cos104"> cos104 <br>
<input type="checkbox" name="course" value="cos105"> cos105 <br><br>
<input type="submit">

asp code..(result.asp)

sql = "SELECT * FROM table where "_
& "And course Like '%" & Request.QueryString("course") & "%' ;"

then will display the courses you choose for the semester from Access database
for example like this link to choose the topic, then display...

Any suggestion?

05-10-2006, 02:25 PM
If you submit checkboxes with the same name then the values will produce a comma delimited string.

You can then split it and construct a query.

05-11-2006, 03:32 PM
Is the database need to change? like cos101, cos102... use yes/no check (different field but same table)?
or can just put all or either together etc. cos101, cos102, cos103 for spring semester; cos102, cos103, cos105 for summer semester...?

if I split the checkbox then will be like cos101 cos102.. I understand that but didn't display the result what I want...

Any example? I check asp101, and google search, not much help.

I know mboxcar's search from the database, mine is through the html page, is all the same? or should I pull the checkbox value from the database(different table)?


05-11-2006, 05:28 PM
mm.. after testing..

I got the error message...

Microsoft VBScript runtime error '800a01a8'
Object required: "

any idea?


05-11-2006, 06:58 PM
Ok, never mind.. i forgot the object... :eek: fixed.

BUT.. :mad:
The resut frpm cosresult.asp display..

SELECT * FROM table WHERE course IN ('cos101')
Sorry! Record not found!!

at least display like:
Semester Course
Spring cos101, cos102
Summer cos101, cos103

How come No record found?? :(

any idea?

p.s. maybe I should write the new Thread too...

05-11-2006, 07:34 PM
You can use the SQL IN clause on a comma delimited string that isn't numeric which is querying a non numeric field with a minor modification.

just change the value of the string like so

sMyString = replace(request.form("widgets"), ",", "','")

you have now added the single quotes to allow the search

sSQL = "SELECT * FROM table WHERE course IN ('" & sMyString & "');"

05-11-2006, 07:55 PM
I know...
I used/copied the Split code just like degsy's code
It does do the result

select * from table where course IN ('cos101', 'cos102')

but still No record found..

should at least show/display
Spring: cos101, 102

ok, here is my table from access database ( I think table needs redesign..)
Semester (text) | Course (text)
Spring | cos101, cos102
Summer | cos101, cos103
Fall and winter etc...

......:confused: :(

Any example? I can test and see how it works or post my 2 pages code here so can tell me whats wrong and database too...

Thanks for help..

05-11-2006, 08:19 PM
is this the result of a response.write to the screen? or is it hard coded in the sql statement with no variable? if neither is true, then do a response.write of your sql statement to the screen to ensure that that is indeed what the sql statement is.

select * from table where course IN ('cos101', 'cos102')

05-11-2006, 09:58 PM
Yes, its from the response.write sql

cause I wanna check the result page w/ sql, make sure the statement is right.

Let me post my code...
here is the page1.html

Search the course and see which semester you would like to attend:
<form action="cosresult.asp" method="get">
<input type="checkbox" name="course" value="cos101"> cos101
<input type="checkbox" name="course" value="cos102"> cos102
<input type="checkbox" name="course" value="cos103"> cos103
<input type="checkbox" name="course" value="cos104"> cos104
<input type="checkbox" name="course" value="cos105"> cos105
<input type="checkbox" name="course" value="cos106"> cos106
<input type="checkbox" name="course" value="cos107"> cos107
<input type="submit">

here is cosresult.asp...

Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "cossearch"
You search for Course: <%= Request.QueryString("course") %>


dim scourse, myRS, sql

If Len(Request.QueryString) > 0 Then
scourse = Request.QueryString("course")
If Not IsEmpty(scourse) Then
scourse = Split(scourse, ",")
Item = Ubound(scourse)
For x=0 To Item
xcos = xcos & "'" & scourse(x) & "',"
xcos = Left(xcos, Len(xcos)-1)

sql = "SELECT * FROM table where course IN (" & xcos & ")"

Set myRS = Server.CreateObject ("ADODB.Recordset")

myRS.Open sql, objConn

response.write sql & "<p>"

If myRS.EOF then
response.write "<font color='red'>Sorry! No Record Found!!</font>"


<table border="1">
<td bgcolor="#009966" valign="middle" align="left"><font color="#ffffff"><b>Semester</b></td>
<td bgcolor="#009966" valign="middle" align="left"><font color="#ffffff"><b>Course</b></td>

<%While Not myRS.EOF %>
<td><%= myRS("semester") %> &nbsp;</td>
<td><%=myRS("course")%> &nbsp;</td>

End If 'end of checkbox not empty
End If 'end of request.querystring >0
Set myRS = Nothing
Set objConn = Nothing
End If


Is this make sense? or my database table is wrong...
The result is: No Record Found!

If the sql statement for search I used/tried:

dim scourse
scourse = Request.QueryString("course")

Dim myRS, sql

sql = "SELECT * FROM table where "_
& "And course Like '%" & scourse & "%' ;"

will do the search but only display the ones I chosen... :confused:
like if I choose: cos101, cos102
then display:
Spring cos101, cos102

how about Summer cos101, cos103 ? -- no display, but I want it display too.

If I choose cos101, cos103
then NO record found!
Sould it display like:
Spring cos101, cos102
Summer cos102, cos103

How come No record found?? :mad:

Is there any example or website I can look at? or Is my code bad? :eek:


05-12-2006, 12:21 AM
and if you open your access database and run that same query does it return any results? I am betting not.

The query IN is looking for each value from the array to be the value in the course column. But you do not have that as the value you have the following as values :
cos101, cos102
cos102, cos103

the string "cos101" is not the same as this string "cos101, cos102"
That is why your LIKE query worked.
using the IN query says to look to see if the string "cos101" is the same as this string "cos101, cos102" or this string "cos102, cos103"
plus to check if the string "cos102" is the same as this string "cos101,cos102" or this string "cos102, cos103"

what does the database consist of for tables? is the value cos101 a pointer to another table? out of curiosity is this a table for a course catalog similar to something a school might have? if so I have a better solution for you.

05-12-2006, 04:31 AM
yes.. something like that....

the cos101, cos102, cos103.. should point out Other table but I put them all together thought would be easier for searching... guess not! :mad:
and stuck!! no matter what I serached from Google or all the asp forums, no answer what I want.. I only fount some website but they used code fusion...
well.. see this website:
click the checkboxes then will see the result... I am wondering if I can do something like that.. but ohh well again.. my search only searched ONE, or stick together not seperate the results I want.. like array{1,2,3,4,5} can find 1 or 2 or 3 or 4 or 5, but can't find {1, 3} but can find {1,2} or {2,3}.. etc.

so far I only tried checkboxes.... if you have the example that would be great, I will give you my email. But I do wannt Find Why :confused: ... drive me crazy but still alive :cool:

and yes, maybe my database got the major problem and you were right.. the database query not found any either :( maybe should break the table would be easier? :rolleyes:

Thanks again,

05-12-2006, 03:24 PM
yes the database design is the problem. I am guessing that what you need is another table with only two columns. Then you put a single value in each field. Generally these would be the values of the primary keys from other tables.

It looks like you are doing an online course catalog.

at the bare minimum you should have in place a table listing each course and a table listing the semesters/quarters Most likely you also have a table listing instructors as well as departments etc.... Now you need to add a table that I mentioned above. This table will point to the primary key from each of the other two tables. It will only have a single value in each column for each row. so what you will have now is records that look something like what I have outlined below:

semester table
1 Spring
2 Summer
3 Fall
4 Winter

course table
101 Cos101
102 Cos102
103 Cos103
104 Cos104

Foreign key table
1 101
1 103
2 102
2 103

While this will have a lot of rows, the actual size will not be that large especially if the primary keys to these fields is a numeric value.

Now you can do your search as you listed and it will work.

05-12-2006, 03:32 PM
Ok, I will try it first... :o
also my email is myeh71@yahoo.com If you have the example, so I can take a look. Will let you know how's going.

Thanks again,

05-12-2006, 03:36 PM
I will try to look back later but I have to get back to work... I have a lot of code to write today.

05-12-2006, 03:55 PM
Cool :)
I will let you know what's going on after redesign my tables.


05-15-2006, 08:20 PM
I found the solution and changed my table instead of one big table....
now the search is fine and ok.
But still if have any online catalog courses example welcome to send to me so I can check it out and improve my skill.

Thanks everyone :)