home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / checkbox.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  5.8 KB  |  194 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 checkbox form elements:</h2>
  14.  
  15. <form action="<%= Request.ServerVariables("URL") %>" method="get">
  16.  
  17.   A Default No-Frills Checkbox:
  18.   <input type="checkbox" name="default" />
  19.  
  20.   <br />
  21.  
  22.   Checkbox With An Associated Value:
  23.   <input type="checkbox" name="value_specified" value="Some Value" />
  24.  
  25.   <br />
  26.  
  27.   Multiple Checkboxs With The Same Name:
  28.   <input type="checkbox" name="multiple" value="1" />
  29.   <input type="checkbox" name="multiple" value="2" />
  30.   <input type="checkbox" name="multiple" value="3" />
  31.  
  32.   <br />
  33.  
  34.   Checkbox Which Maintains It's State:
  35.   <input type="checkbox" name="state_keeper"
  36.   <%
  37.   ' This is the code that keeps the state of the checkbox.
  38.   ' All it does is check to see if the value returned is
  39.   ' the same as the value parameter of the checkbox input
  40.   ' tag.  If so, the box was checked and we output the
  41.   ' input tag's "checked" attribute to keep it that way.
  42.   If Request.QueryString("state_keeper") = "on" Then
  43.     Response.Write " checked=""checked"""
  44.   End If
  45.   %>
  46.   />
  47.  
  48.   <br />
  49.   <br />
  50.  
  51.   <input type="submit" />
  52.  
  53. </form>
  54.  
  55.  
  56. <br />
  57. <br />
  58. <br />
  59.  
  60.  
  61. <h2>Let's check out our values just submitted:</h2>
  62. <blockquote><em>
  63. FYI: Note that the above form submits back to this same page.
  64. If you haven't yet submit the form then there won't be
  65. any values in the text below since they are retreived
  66. from the form.  In this case, the values reflected are
  67. simply the defaults and are the same results you would
  68. get if you submitted the form without checking anything.
  69. </em></blockquote>
  70.  
  71. <p><strong>The Default No-Frills Checkbox</strong></p>
  72. <p>
  73. By default, an HTML checkbox element returns nothing if it
  74. wasn't checked and a value of "on" if it was.
  75. So by checking it's value we can determine if it was
  76. checked or not when the form was submitted.
  77. The No-Frills Checkbox was: <strong>
  78. <%
  79. ' Note that "on" is in lower case.  Since writing "on"
  80. ' doesn't do much good, I simply check for it to determine
  81. ' which branch of the conditional to execute.  I could
  82. ' place whatever code I wanted inside either condition.
  83. ' In this case I simply print out a "user-friendly" message.
  84. If Request.QueryString("default") = "on" Then
  85.     Response.Write "Checked"
  86. Else
  87.     Response.Write "Not Checked"
  88. End If
  89. %>
  90. </strong>.
  91. </p>
  92.     
  93. <p><strong>Checkbox With An Associated Value</strong></p>
  94. <p>
  95. If you don't like the fact that the checkbox returns the
  96. value of "on" it's really easy to change.  Simply
  97. add a value parameter and assign to it what you'd like the
  98. value returned by the checkbox to be.
  99. The Checkbox With An Associated Value returned:
  100. <strong><%= Request.QueryString("value_specified") %></strong>.
  101. </p>
  102.  
  103.  
  104. <p><strong>Multiple Checkboxs With The Same Name</strong></p>
  105. <p>
  106. Probably one of the most useful uses of the checkbox is to
  107. allow users to check multiple selections.  Instead of having
  108. to chech each box's status individually you can simply name
  109. them all the same and the results will be returned in a
  110. convenient collection like fashion.
  111. </p>
  112. <p>
  113. You can access them in a number of ways.  First you can use
  114. the "For Each" syntax illustrated here
  115. (in the source code at least):
  116. <strong>
  117. <%
  118. Dim Item
  119.  
  120. For Each Item In Request.QueryString("multiple")
  121.     ' Do Whatever you want to do with each one.
  122.     ' I'll just write them out with spaces after
  123.     ' each one so they don't run together.
  124.     Response.Write Item & " "
  125. Next
  126. %>
  127. </strong>.
  128. </p>
  129. <p>
  130. Along the same lines you can access them by index:
  131. <strong>
  132. <%
  133. Dim I
  134.  
  135. For I = 1 To Request.QueryString("multiple").Count
  136.     ' Again I'm just writing them out.
  137.     Response.Write Request.QueryString("multiple")(I) & " "
  138. Next
  139. %>
  140. </strong>.
  141. </p>
  142. <p>
  143. And perhaps the easiest way is to just get them
  144. as a comma delimited list:
  145. <strong>
  146. <%= Request.QueryString("multiple") %>
  147. </strong>.
  148. </p>
  149.  
  150. <p><strong>Checkbox Which Maintains It's State</strong></p>
  151. <p>
  152. At this point there's really nothing different about
  153. this checkbox from any other, but I thought I should
  154. illustrate the technique when building the form
  155. (see the form generating section of the source code)
  156. because it's so useful in real life.  In particular, when
  157. you're sending a user back to the form to check their
  158. entries, it's really frustrating for them if they have
  159. to re-enter all their values and re-check all the boxes.
  160. </p>
  161. <p>
  162. In terms of the value, like I said, this is just like the
  163. standard checkbox, but just to keep up the pattern...
  164. Checkbox Which Maintains It's State returned:
  165. <strong><%= Request.QueryString("state_keeper") %></strong>.
  166. </p>
  167.  
  168.  
  169. <%
  170. ' Here's a little function that you might find handy.
  171. ' It takes the name parameter of the checkbox you want
  172. ' to check and will return a boolean indicating if a
  173. ' checkbox with that name was checked on the form when
  174. ' the user submitted it even if the value of the
  175. ' checkbox isn't set to anything.  It works well to
  176. ' help simplify your code... especially in conditionals.
  177. Function IsChecked(strFieldName)
  178.   Dim blnChecked
  179.  
  180.   If Request.QueryString(strFieldName).Count > 0 Then
  181.     blnChecked = True    
  182.   Else
  183.     blnChecked = False
  184.   End If
  185.   
  186.   IsChecked = blnChecked
  187. End Function
  188.  
  189. ' For example, using the state_keeper example above:
  190. 'If IsChecked("state_keeper") Then
  191. '  ' The checkbox was checked.
  192. 'End If
  193. %>
  194.