home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / radiobutton.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  5.4 KB  |  164 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <h2>Some sample radio button form elements:</h2>
  14.  
  15. <form action="<%= Request.ServerVariables("URL") %>" method="get">
  16.  
  17.   A Default No-Frills Radio Button:
  18.   <input type="radio" name="default" />
  19.  
  20.   <br />
  21.  
  22.   Radio Button With An Associated Value:
  23.   <input type="radio" name="value_specified" value="Some Value" />
  24.  
  25.   <br />
  26.  
  27.   Multiple Radio Buttons With The Same Name:
  28.   <input type="radio" name="multiple" value="1" />
  29.   <input type="radio" name="multiple" value="2" />
  30.   <input type="radio" name="multiple" value="3" />
  31.  
  32.   <br />
  33.  
  34.   Radio Button Which Maintains It's State:
  35.   <input type="radio" name="state_keeper"
  36.   <%
  37.   ' This is the code that keeps the state of the radio
  38.   ' button.  All it does is check to see if the value
  39.   ' returned is the same as the value parameter of the
  40.   ' radio button input tag.  If so, the radio button was
  41.   ' enabled and we output the input tag's "checked"
  42.   ' attribute to keep it that way.
  43.   If Request.QueryString("state_keeper") = "on" Then
  44.     Response.Write " checked=""checked"""
  45.   End If
  46.   %>
  47.   />
  48.  
  49.   <br />
  50.   <br />
  51.  
  52.   <input type="submit" />
  53.  
  54. </form>
  55.  
  56.  
  57. <br />
  58. <br />
  59. <br />
  60.  
  61.  
  62. <h2>Let's check out our values just submitted:</h2>
  63. <blockquote><em>
  64. FYI: Note that the above form submits back to this same page.
  65. If you haven't yet submit the form then there won't be
  66. any values in the text below since they are retreived
  67. from the form.  In this case, the values reflected are
  68. simply the defaults and are the same results you would
  69. get if you submitted the form without checking anything.
  70. </em></blockquote>
  71.  
  72. <p><strong>The Default No-Frills Radio Button</strong></p>
  73. <p>
  74. By default, an HTML radio button element returns nothing
  75. if it wasn't enabled and a value of "on" if it
  76. was.  So by checking it's value we can determine if it was
  77. enabled or not when the form was submitted.
  78. The No-Frills Radio Button was: <strong>
  79. <%
  80. ' Note that "on" is in lower case.  Since writing "on"
  81. ' doesn't do much good, I simply check for it to determine
  82. ' which branch of the conditional to execute.  I could
  83. ' place whatever code I wanted inside either condition.
  84. ' In this case I simply print out a "user-friendly" message.
  85. If Request.QueryString("default") = "on" Then
  86.     Response.Write "Enabled"
  87. Else
  88.     Response.Write "Disabled"
  89. End If
  90. %>
  91. </strong>.
  92. </p>
  93.     
  94. <p><strong>Radio Button With An Associated Value</strong></p>
  95. <p>
  96. If you don't like the fact that the radio button returns the
  97. value of "on" it's really easy to change.  Simply
  98. add a value parameter and assign to it what you'd like the
  99. value returned by the radio button to be.
  100. The Radio Button With An Associated Value returned:
  101. <strong><%= Request.QueryString("value_specified") %></strong>.
  102. </p>
  103.  
  104.  
  105. <p><strong>Multiple Radio Buttons With The Same Name</strong></p>
  106. <p>
  107. Similarly to the checkbox, the scenario where you have a set
  108. of radio buttons all with the same name is probably one of the
  109. most useful ways to use this type of input tag.  The main
  110. difference is that instead of allowing a user to specify
  111. multiple answers to one question, the radio button element
  112. ensures that one and only one selection is choosen from among
  113. the group of buttons that share a common name.
  114. Multiple Radio Buttons With The Same Name ensured that only one
  115. choice was selected and it was:
  116. <strong><%= Request.QueryString("multiple") %></strong>.
  117. </p>
  118.  
  119. <p><strong>Radio Button Which Maintains It's State</strong></p>
  120. <p>
  121. At this point there's really nothing different about
  122. this radio button from any other, but I thought I should
  123. illustrate the technique when building the form
  124. (see the form generating section of the source code)
  125. because it's so useful in real life.  In particular, when
  126. you're sending a user back to the form to check their
  127. entries, it's really frustrating for them if they have
  128. to re-enter all their values and re-select all the appropriate
  129. radio buttons.
  130. </p>
  131. <p>
  132. In terms of the value, like I said, this is just like the
  133. standard radio button, but just to keep up the pattern...
  134. Radio Button Which Maintains It's State returned:
  135. <strong><%= Request.QueryString("state_keeper") %></strong>.
  136. </p>
  137.  
  138.  
  139. <%
  140. ' Here's a little function that you might find handy.
  141. ' It takes the name parameter of the radio button you want
  142. ' to check and will return a boolean indicating if a
  143. ' radio button with that name was enabled on the form when
  144. ' the user submitted it even if the value of the
  145. ' radio button isn't set to anything.  It works well to
  146. ' help simplify your code... especially in conditionals.
  147. Function IsChecked(strFieldName)
  148.   Dim blnChecked
  149.  
  150.   If Request.QueryString(strFieldName).Count > 0 Then
  151.     blnChecked = True    
  152.   Else
  153.     blnChecked = False
  154.   End If
  155.   
  156.   IsChecked = blnChecked
  157. End Function
  158.  
  159. ' For example, using the state_keeper example above:
  160. 'If IsChecked("state_keeper") Then
  161. '  ' The radio button was enabled.
  162. 'End If
  163. %>
  164.