Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > HTML > HTML Code Clinic
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 19th, 2003, 12:28 PM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
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?
Reply With Quote
  #2 (permalink)  
Old June 20th, 2003, 07:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
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
Reply With Quote
  #3 (permalink)  
Old June 20th, 2003, 08:25 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
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!
Reply With Quote
  #4 (permalink)  
Old June 20th, 2003, 11:21 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
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!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:55 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.