I am running into a problem with my vbscript code. My HTML code looks like this
<input type='checkbox' name='DisplayRow' id='DisplayRow1' />
<input type='checkbox' name='DisplayRow' id='DisplayRow2' />
<input type='checkbox' name='DisplayRow' id='DisplayRow3' />
This is done because above there is another checkbox that calls a javascript function that will check or uncheck all of the "DisplayRow" checkboxes. The javascript function uses getElementsByName to return all of the checkboxes named "DisplayRow".
When the form's submit button is clicked the action sends it to an ASP page (classic ASP) that grabs all of the objects on the calling form by using the Request.Form command. The Request.Form command looks at the "name" attribute, not the "id" attribute of the object.
This seems to be working fine for all of the other form objects. When it gets to the checkboxes because the "name" attribute uses the same name for all of the check boxes it returns an array. The individual checkboxes can be accessed like this:
Request.Form("DisplayRow")(x)
Where x references the individual checkbox in the array.
If the checkboxes are checked I can loop through the array without any problems. If 1 or more or all of the checkboxes are unchecked then when the code references the first checkbox in the array that is unchecked the page crashes. Nothing is executed after the Request.Form command fails.
I have tried enclosing the Request.Form("DisplayRow")(x) in an IsNull function in an If statement but it still takes the program down.
Has anyone else ran into this and found a work around?
Edit
For some reason stackoverflow is not letting me add more than one comment.
@Cory. Thanks for the information
@jwatts1980. Count works but it does not let me know which of the checkboxes are checked. If the count is greater than 0 I can loop through them but if the first one is unchecked I am right back where I started with a crashed page.
You cannot do it this way because unchecked checkboxes will not be submitted in the post, only the checked ones.
I would approach this differently:
First I would add a value
attribute to the checkboxes, as so:
<input type='checkbox' name='DisplayRow' id='DisplayRow1' value="1" />
<input type='checkbox' name='DisplayRow' id='DisplayRow2' value="2" />
<input type='checkbox' name='DisplayRow' id='DisplayRow3' value="3" />
Notice the value is the same as the index, this will be needed in the code.
Next I would use .Split()
to gather the checkboxes selected in an array, since the values will come in as a string separated by comma, ie: 1,2,3
to your .Form()
value.
Rows = Request.Form("DisplayRow")
'check if any are selected first
If Rows <> "" Then
'make an array with each value selected
aRows = Split(Rows,",")
'loop through your array and do what you want
For i = lBound(aRows) to uBound(aRows)
Response.Write "DisplayRow" & aRows(i) & " was Selected."
Next
End If
This way you only process the results for the one's selected, and ignore the others.
I've always found this essential for my library...
'GetPostData
' Obtains the specified data item from the previous form get or post.
'Usage:
' thisData = GetPostData("itemName", "Alternative Value")
'Parameters:
' dataItem (string) - The data item name that is required.
' nullValue (variant) - What should be returned if there is no value.
'Description:
' This function will obtain the form data irrespective of type (i.e. whether it's a post or a get).
'Revision info:
' v0.1.2 - Inherent bug caused empty values not to be recognised.
' v0.1.1 - Converted the dataItem to a string just in case.
function GetPostData(dataItem, nullVal)
dim rV
'Check the form object to see if it contains any data...
if request.Form = "" then
if request.QueryString = "" then
rV = nullVal
else
if request.QueryString(CStr(dataItem))="" then
rV = CStr(nullVal)
else
rV = request.QueryString(CStr(dataItem))
end if
end if
else
if request.Form(CStr(dataItem)) = "" then
rV = CStr(nullVal)
else
rV = request.Form(CStr(dataItem))
end if
end if
'Return the value...
GetPostData = rV
end function
To use the function you simply pass in the form or query string item you're looking for and what to replace empty values with...
Dim x
x = GetPostData("chkFirstOne", false)
You can keep using your existing code by adding a single condition:
If Request.Form("DisplayRow").Count > 0 Then
'your code here
Else
'consider adding message saying nothing was selected
End If