Solved

VBScript Server Side Code to read checkbox.ID

Posted on 2009-05-14
2
301 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dreamweaver server behavior gone 6 137
ASP SQL Syntax Duplicate Key 7 102
age from date of birth 4 41
Adding Row and Cell on a table 14 18
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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

776 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