Looping through form elements, I'm stuck, help!

HarpuaFSB
HarpuaFSB used Ask the Experts™
on
Hey all,

This is simple enough, I've done it before but the solution is eluding me and it's driving me nuts.  Anyway, in ASP VBScript, I'm looping through a recordset and creating checkboxes:

<INPUT TYPE="CHECKBOX" NAME="chk<% = rsSystem("OrderNumber") %>" VALUE="-1">

Now when I post the form, I need to loop through all checkbox items that were created and retrieve the name and value of each item.

I think I have tried every form of "For Each Item in Request.Form..." I could think of and have had zero success getting the info I'm looking for.

Any ideas?

Thanks,
Tom
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
you wrote:

  [rsSystem("OrderNumber")]

then:

  [I think I have tried every form of "For Each Item in
   Request.Form...]

are you looping through an item in the Request.Forms collection or a recordset? can you post more of your ASP code? pehaps you're not passing/accessing your form data correctly.

hope i can help.

matt

Commented:
First of all, check the HTML of your form page. If all the checkboxes have the SAME name, there's only ONE value being passed so there's nothing to cycle through.

If the checkboxes have different names, let us know -- I know I have this on one of my sites, and I can post the code I use.

Author

Commented:
matticus,

Looping through the Request.Forms collection.

I just did a workaround using radio buttons instead but am still interested in the solution for this problem...

Webwoman is onto what I was talking about...

all the checkboxes had different names based on the unique ID from the recordset I was looping through (i.e., order # 190, created a checkbox item named "chk190" value="-1")...then when the user submits the form (using the post method) I need to loop through all the chk___ items and, here's the tricky part, checked OR not, retrieve the item's name and value.

so I'd need to retrieve something like this:
chk190 "" (where "" = blank or not checked)
chk191 -1
chk192 -1
chk193 ""
...etc.

make any more sense?

Thanks...
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Author

Commented:
matticus,

Looping through the Request.Forms collection.

I just did a workaround using radio buttons instead but am still interested in the solution for this problem...

Webwoman is onto what I was talking about...

all the checkboxes had different names based on the unique ID from the recordset I was looping through (i.e., order # 190, created a checkbox item named "chk190" value="-1")...then when the user submits the form (using the post method) I need to loop through all the chk___ items and, here's the tricky part, checked OR not, retrieve the item's name and value.

so I'd need to retrieve something like this:
chk190 "" (where "" = blank or not checked)
chk191 -1
chk192 -1
chk193 ""
...etc.

make any more sense?

Thanks...

Commented:
checkboxes that are *not* checked in the browser, will *not* be part of the form elements posted.

if there is more then one form-element with the same name, you can loop through those with:

For each element in Request.Form("thesamename")
    Response.write element & "<br>"
Next
(in this case the "element" is the value)

Response.write Request.Form("thesamename") & "<br>"
will result in a string with all values, seperated with ", " (comma space).



Commented:
For a better understanding, you should realize that the form-object, is a collection (you can loop through all elements) of sub-objects which are also collections.

The interesting thing is that the default value of those objects is a string.

Try this:

Response.write Typename(Request.Form)
- it says IRequestDictionary (and not String)

Response.write Typename(Request.Form("some-element"))
- it says IStringList (and not String)

But for both:
Response.write Vartype(Request.Form)
Response.write Vartype(Request.Form("some-element"))
will return 8, and vartype 8 means String.

The IRequestDictionary is a sort of Dictionary-object, in which each key contains a IStringList.

The IStringList is a sort of array of strings.










Commented:
And last comment:

Harpua, for your purpose you should probably give all checkboxes the same name (let's say "chk") and a different value.
The values you do not find back in the Request.Form("chk") are from the checkboxes that were not checked.

Author

Commented:
matticus,

Looping through the Request.Forms collection.

I just did a workaround using radio buttons instead but am still interested in the solution for this problem...

Webwoman is onto what I was talking about...

all the checkboxes had different names based on the unique ID from the recordset I was looping through (i.e., order # 190, created a checkbox item named "chk190" value="-1")...then when the user submits the form (using the post method) I need to loop through all the chk___ items and, here's the tricky part, checked OR not, retrieve the item's name and value.

so I'd need to retrieve something like this:
chk190 "" (where "" = blank or not checked)
chk191 -1
chk192 -1
chk193 ""
...etc.

make any more sense?

Thanks...

Author

Commented:
Never thought of that, using the same name and assigning the value uniquely.

Good call!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial