Solved

VBScript Server Side Code to read checkbox.ID

Posted on 2009-05-14
2
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
Bobaran98 earned 500 total points
ID: 24391806
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
ID: 31581608
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Query not working correctly? (or how i want it to... 6 54
Writing comments on <p></P> or paragraph 2 23
JQuery Autocomplete Tag AJAX (Need nice script) 11 57
ASP AND XML 3 30
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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