Link to home
Start Free TrialLog in
Avatar of lshane
lshane

asked on

How to create a loop for checkbox values passed from a form

DWMX
WIN XP PRO
Classic ASP VBScript
MS Access

Hello.  I have a basic search form that searches by State from a dropdown list of states (testsales.asp)

It searches a table named "sale_listings" in the db (testsearchresults.asp) and displays a repeating region of all sales in the recordset.

On the "results" page, I have a checkbox next to each sale that is displayed from the recordset.
I bound the checkbox to the "sale_ID" of the recordset.

The checkboxes are there so I can select whichever sales I want and then be sent to a Printer-Friendly page to display only the sales I checked.

Question:
How can I do this?
I used the "GET" method and I see all the "sale_ID" sales in the URL that I selected, but I get an error on the Printer-Friendly page where I just have a recordset querying for "sale_ID" in the request.queystring.

I would imagine I would need some sort of loop to loop through all "sale_ID" sales in the URL, but I'm not sure how to do that.

Here's the page for reference.  Just do a search on "GA" and you will see some sales:
<http://www.thebargainwatcher.com/testsales.asp>

Thank you so much in advance.

Shane
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

lshane,

For a loop try ...

var arIDs = Split(Request.Querystring("sale_ID"), ", ")
For each item in arIDs
    ' what you want to do in loop
    '    use "item" to access each value that was checked
Next

Let me know if you have any questions or need more information.

b0lsc0tt
Avatar of lshane
lshane

ASKER

Hi, b0lsc0tt.  Thanks so much.  I am having a difficult time knowing where to place the code.

I will attach the entire code from the page (including yours).  It's only a raw, basic page, so it's not too much.

The recordset on the page is filtering for "sale_ID" in the querystring also.  I'm not sure if I have to alter that.  I'm sure you'll tell me.


<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/connbargain.asp" -->
<%
Dim rsPFSales__MMColParam
rsPFSales__MMColParam = "1"
If (Request.QueryString("sale_ID") <> "") Then 
  rsPFSales__MMColParam = Request.QueryString("sale_ID")
End If
%>
<%
Dim rsPFSales
Dim rsPFSales_numRows
 
Set rsPFSales = Server.CreateObject("ADODB.Recordset")
rsPFSales.ActiveConnection = MM_connbargain_STRING
rsPFSales.Source = "SELECT * FROM sale_listings WHERE sale_ID = " + Replace(rsPFSales__MMColParam, "'", "''") + ""
rsPFSales.CursorType = 0
rsPFSales.CursorLocation = 2
rsPFSales.LockType = 1
rsPFSales.Open()
 
rsPFSales_numRows = 0
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
<table width="500" border="0" cellspacing="0" cellpadding="0">
<%
var arIDs = Split(Request.Querystring("sale_ID"), ", ")
For each item in arIDs
  <tr>
    <td><%=(rsPFSales.Fields.Item("sale_title").Value)%></td>
  </tr>
    '    use "item" to access each value that was checked
Next
%>
 
</table>
 
</body>
</html>
<%
rsPFSales.Close()
Set rsPFSales = Nothing
%>

Open in new window

Are you getting the results in the recordset that you need?  If so I don't see why you use the querystring results again.  They are in the recordset as the field sale_ID.

If the recordset also isn't working then you need to use IN in the SQL query.  When you are looking for results matching more than 1 sale_ID then that is the way to do it.  What type of field (in the DB) is sale_ID?

Basically the SQL would look like ...

SELECT * FROM sale_listings WHERE sale_ID IN (id1, id2, id3)

Depending on the field type (in the DB) and the DB you use you may need delimit the values in the parentheses.  You could use the split to help make this.  In some case a loop may be needed to add the delimiter so let me know the type of DB and the type of field.

Then in the html just use the recordset and its results.

Let me know how this helps or if you have a question.  If you need help with the script then please answer the things I asked.

bol
Avatar of lshane

ASKER

Hi, b0l.  Cool - thank you.

The "sale_ID" is an "autonumber" field.
The DB is MS Access.

I tried just creating a basic recordset such as:  
SELECT * FROM sale_listings WHERE sale_ID = MMColParam

(  Where MMColParam = "request.querystring("sale_ID")"  )

Then I applied a Repeat Region to the row.

However, it did not work... probably because several "sale_ID" parameters were coming across the URL in a concatenated fashion.

The SQL you showed looks more like what I need, but I'm uncertain how to type it, or enter it; so I'll definitely need your assistance with that, please.

ASKER CERTIFIED SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lshane

ASKER

Thank you so much, b0l.

I'll try it when I get home this evening - in 2 or 3 hours.

I'll let you know.

Thank you,
Shane
Avatar of lshane

ASKER

Hi, b0l.  

OK, I tried it, and I continue to get errors like "Too few parameters...expected 1...".

I'm placing the following code in the SQL box in DWMX:
===================================================
SELECT *
FROM sale_listings
WHERE sale_ID IN ("& MMColParam &")
===================================================
MMColParam is equal to "request.querystring("sale_ID")".

I tried it with single quotes, no quotes, no parentheses, etc.
Still get errors.

Still needing help.
Thanks so much.
Make sure Request.Querystring("sale_ID") has a value first.  Add something like ...

If Request.Querystring("sale_ID") = "" then
    Response.Write "There are no ids."
    Response.End
End if

That block would go before the code to use the SQL.  If you see that message then you will know there were no values to use.

If you still have a problem or question then please post your current code, including the part where you set MMColParam.

bol
Avatar of lshane

ASKER

Hi, bol.  Sorry for the delay.  I didn't see your post come through on my E-mail.

I'll post the code tomorrow.

I was getting the "Too few parameters..." message when I pressed the "TEST" button in the SQL box of DWMX.  Typically I can just enter a default value and press "TEST" and if there are no ID's, it would just say "No Data".  This is giving me some kind of error because it thinks something is missing.

I'll study it a little more tomorrow and post the code.

Thank you SO much,
Shane
Avatar of lshane

ASKER

Sorry for the delay.  Been going crazy.  I think I can make this work.

Thanks so much,
Shane
Your welcome!  Thanks for the fun question, the grade and the points.  I'm glad I could help.  I'll see you around. :)

bol
Avatar of lshane

ASKER

Hi, b0l.  I was able to get the selections to appear on the Printer-Friendly page after selecting the checkboxes, but now I'm having issues with getting them to separate, as opposed to being in a row.

The following is my objective layout:

Sale Title
Start Date - End Date
Driving Directions
<HR>

I will post this to another thread, but I wanted to give you a heads-up, if you wanted to try and assist me.  Thanks so much.