Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > HTML > HTML Code Clinic
|
HTML Code Clinic Do you have some HTML code you'd like to share and get suggestions from others for tweaking or improving it? This discussion is the place.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the HTML Code Clinic section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 19th, 2003, 12:28 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default Error getting value from Radio buttons in VBScript

This code works (when there's only one radio button on the form):

<INPUT TYPE=radio NAME="fldAnswer1" VALUE="Question 1 Answer 1">1<BR>

<input type="submit" value="Submit Survey" name="SubmitSurvey"></FORM></BODY>
</HTML><BR><BR>


<SCRIPT LANGUAGE="VBScript">
<!-- Option Explicit
Function frmSurvey_OnSubmit
 msgbox "(" & Document.frmSurvey.fldAnswer1.Value & ")"
End Function

 -->
</SCRIPT>

The msgbox in the OnSubmit function returns "Question 1 Answer 1" as the result (which I find puzzling since the radio button is NOT selected when I hit the Submit button, but that's the next problem if I can get past the current one).

This code, however, does NOT work (with two Radio buttons using the same name but containing different values):

<INPUT TYPE=radio NAME="fldAnswer1" VALUE="Question 1 Answer 1">1<BR>
<INPUT TYPE=radio NAME="fldAnswer1" VALUE="Question 1 Answer 2">2<BR>

<input type="submit" value="Submit Survey" name="SubmitSurvey"></FORM></BODY>
</HTML><BR><BR>


<SCRIPT LANGUAGE="VBScript">
<!-- Option Explicit
Function frmSurvey_OnSubmit
 msgbox "(" & Document.frmSurvey.fldAnswer1.Value & ")"
End Function

 -->
</SCRIPT>

The code above produces an error window that says: "Error: Object doesn't support this property or Method: "Document.frmSurvey.fldAnswer1.Value'". It's the EXACT SAME reference as in the working example above. The only difference is that there are TWO radio buttons being created on the form instead of one. Is there some difference when working with a "control array" as opposed to a single field, or is my def'n of multiple radio buttons above somehow incorrect?
 
Old June 20th, 2003, 07:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Ron,

As you guessed you must treat a control array differently by looping through all the controls in the array and inspecting their checked property, like this:
Code:
  Dim n
  For n = 0 to Document.frmSurvey.fldAnswer1.length - 1
    If Document.frmSurvey.fldAnswer1(n).checked Then
      msgbox "(" & Document.frmSurvey.fldAnswer1(n).Value & ")"
    End If
  Next
I think in your single-button code you also need to inspect the checked property, otherwise you'll probably get the value displayed whether or not the button has been clicked.

rgds
Phil
 
Old June 20th, 2003, 08:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Phil,

Thanks so much for your help. I'd hoped that since it is a radio button the .Value property might contain the selected value and would be blank otherwise. I'd not have guessed I'd need the .Length property, either - experience with VB would've had me searching for a .ListCount property to loop through the values. It's hard finding documentation for how VBScript works (and it is a very different world from VB, imho). Thanks again for your help!
 
Old June 20th, 2003, 11:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

For "enquiring" minds, here's what I've discovered.

This VBScript REQUIRED FIELD validation logic works CLIENT SIDE when checking an HTML INPUT field or DROPDOWN list with SINGLE VALUE SELECTION:

if Document.formname.fieldname.Value = "" then
    msgbox("Required field missing")
end if

Of course, this just checks to see that a value was entered or selected. More sophisticated chacking against the value provided could be performed, as well. Additionally, it needs mentioning that when working with a drop down list, it's necessary to define a default SELECTED value of "emptystring" or the list will default to the first value in the list and will never fail the REQUIRED field test above. I'm still not sure how to deal with multiple selections in a dropdown list, if that's even possible using the HTML drop down list.

For RADIO controls and CHECKBOXes:

For intPosIdx = 0 To Document.formname.fieldname.length - 1
    If Document.formname.fieldname(intPosIdx).checked Then
        blnSelFd = "Y"

        Exit for
    end if
Next

if blnSelFd = "N" then
    msgbox("Required field missing")
end if

In the example above, it's necessary to loop through all the potential values to see if any are checked, setting a variable if they are. After, I check the variable tyo see whether a selection was made. While it works with both RADIO controls and CHECK BOXes, this logic is obviously not concerned with whether single or multiple selections have been made. More sophisticated logic could be added, of course.

Thanks again to Phil for showing me how to do this!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Radio Buttons and Calculations Student101 Java Basics 4 June 21st, 2009 11:02 AM
validation in radio buttons MunishBhatia ASP.NET 2.0 Professional 5 December 11th, 2007 11:15 AM
Validation Radio Buttons jonsey Classic ASP Professional 1 June 6th, 2007 11:48 PM
How to use the radio buttons? ben_VB VB.NET 2002/2003 Basics 1 January 18th, 2005 12:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.