Solved

VBScript Server Side Code to read checkbox.ID

Posted on 2009-05-14
2
296 Views
Last Modified: 2012-06-27
I generate a dynamic set of checkboxes in Classsic ASP

<input type="checkbox" name="cbx1" id="cbx1" value = "abc" >
<input type="checkbox" name="cbx2" id="cbx2" value = "def" >
<input type="checkbox" name="cbx3" id="cbx3" value = "ghi" >

When the ASP Page is posted, I need to loop thru the checkboxes and read their ids in VBScript on the server.

For R = 1 to n
   set myCbx = request.form("cbx" & r)
   myID = myCbx.ID
Next

However the system doesn't like the myID = myCbx.ID line.  Is there any way to read the ID of a checkbox if it is not checked?

Thanks for your help
0
Comment
Question by:deepPowdah
2 Comments
 
LVL 8

Accepted Solution

by:
Bobaran98 earned 500 total points
Comment Utility
If the checkbox is not checked, then it does not get sent.  You can see this in the address line if you configure your form to be submitted with GET.  Assuming the first and third checkboxes are clicked, and your form action is set to submission.asp, you'll see this in your browser's address bar:

Note that cbx2 doesn't get passed at all.  Because it wasn't checked, it does not have a value as far as the submission is concerned.  The same goes if you submit your form via POST.

If you want to always send those three values, rather than making it dependent on the user checking/unchecking them, you could embed them as hidden values.  The user doesn't see the variables, but they still get submitted no matter what.  Example:

<input type="hidden" name="cbx1" id="cbx1" value = "abc" >
<input type="hidden" name="cbx2" id="cbx2" value = "def" >
<input type="hidden" name="cbx3" id="cbx3" value = "ghi" >
As for your loop, I'm not sure why you're processing it that way.  For one thing, you keep overwriting your variable.  For another thing, Request.Form("cbx" & r) is equal to the value tag and "cbx" & r is equal to the ID.  What more do you need to process?

Coming back to your question about knowing which checkboxes were NOT checked, you can certainly have your code determine that if you know what the values were to start with.  Try this... in your first page, the one with the checkboxes, add a hidden field called "cbxNum" and set the value to the number of checkboxes you've put on the page:

<input type="hidden" name="cbxNum" id="cbxNum" value = "3" >

Now when you process the submission, you can do this:

Dim cbxNum as integer
Dim myCbx as string
cbxNum = CInt(Request.Form("cbxNum")

For r = 1 to cbxNum
myCbx = Request.Form("cbx" & r)
If IsNull(myCbx) Or myCbx="" Then
Response.Write("cbx" & r & " was not submitted<br>\n")
Else
Response.Write("cbx" & r & " = " & myCbx & "<br>\n")
End If
Next
0
 

Author Closing Comment

by:deepPowdah
Comment Utility
From what I have been able to determine, an unchecked checkbox does not have an ID or value property.  Only checked checkboxes have these properties.  The solution to my problem, as you suggested, was to create a hidden field in which I can put the vaue I need to keep.  I implementend that and it's sweet.  Thanks for your help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now