CheckBox database search

liunx

Guest
I've tried a few different things and I honestly don't know how to get it to work. I'm using MS VWD Express in hopes it would take care of most of the work. I really hope someone can give me an example of what I'm looking to do.

Just to keep it simple I'll keep it to two columns.

SQL Database
Column 1 ID (int) primary key
Column 2 Name (varchar)

On my aspx page I would like to have checkboxes and to keep the example short let us just say three.

Mike
John
Frank

What I'm look for is if Mike and John are checked I will get a datagrid with results like the following:
ID Name
2 Mike
4 Mike
9 Frank
10 Frank

or if all three are checked I would get
ID Name
1 John
2 Mike
3 John
4 Mike
5 John
6 John
7 John
8 John
9 Frank
10 Frank

I just can't figure it out. Please help. Thanks.I think you use wildcard (%) for that search.

I was wondering, out of curiosity, why don't you use a textbox to search instead? This is what people usually do for a name search.Some SQL u might use in your ASP if u go the textbox route:


strName = Request.Form("txtName")

strSQL = "select ID, name from tblResultData WHERE name like '%" & replace(strName, "'", "''") & %' order by ???"


For checkboxes, give them all names that identify the user, as in:


do until rs_name.eof
response.write ("<input type='checkbox' name='name" & rs_name("ID") & "' />" & rs_name("name"))

rs_name.movenext
loop


Then when u post the form, u might use:


'prepare sql
strIn = "in ("

'loop through form values and find checked checkboxes
for each strKey in Request.Form
if left(strKey, 4) = "name" then
if request.form(strKey) = "on" then
strIn = strIn & mid(strKey, 5, Len(strKey) - 4) & ","
end if
end if
next

'remove final comma and close parentheses
strIn = left(strIn, len(strIn) - 1) & ")"

'will be something like "select ID, name from tblResultData WHERE ID In (2,5,7)...
strSQL = "select ID, name from tblResultData WHERE ID " & strIn & " ORDER BY ???"


Please reply with any questions or comments.

~ mellamokb
 
Back
Top