|
42 | 42 | # |
43 | 43 | # Read on for more details. Examples are provided at the bottom. |
44 | 44 | # |
| 45 | +# == About the Examples |
| 46 | +# |
| 47 | +# Examples on this page assume that \CGI has been required: |
| 48 | +# |
| 49 | +# require 'cgi' |
| 50 | +# |
| 51 | +# Unless otherwise stated, examples also assume that environment variable 'REQUEST_METHOD' exists |
| 52 | +# (which prevents CGI.new from entering its online mode): |
| 53 | +# |
| 54 | +# ENV.include?('REQUEST_METHOD') # => true |
| 55 | +# |
45 | 56 | # == Queries |
46 | 57 | # |
47 | 58 | # The CGI class dynamically mixes in parameter and cookie-parsing |
|
148 | 159 | # Escape and unescape methods are defined in cgi/escape.rb. |
149 | 160 | # And when include, you can use utility methods like a function. |
150 | 161 | # |
151 | | -# == Examples of use |
| 162 | +# == Examples of Use |
152 | 163 | # |
153 | | -# === Get form values |
| 164 | +# === Form Values |
154 | 165 | # |
155 | | -# require "cgi" |
156 | | -# cgi = CGI.new |
157 | | -# value = cgi['field_name'] # <== value string for 'field_name' |
158 | | -# # if not 'field_name' included, then return "". |
159 | | -# fields = cgi.keys # <== array of field names |
160 | | -# |
161 | | -# # returns true if form has 'field_name' |
162 | | -# cgi.has_key?('field_name') |
163 | | -# cgi.has_key?('field_name') |
164 | | -# cgi.include?('field_name') |
| 166 | +# ==== Get Form Values |
165 | 167 | # |
166 | | -# CAUTION! <code>cgi['field_name']</code> returned an Array with the old |
167 | | -# cgi.rb(included in Ruby 1.6) |
| 168 | +# You can use method +cgi.params+ to retrieve form values |
| 169 | +# in a {Hash}[https://docs.ruby-lang.org/en/3.4/Hash.html]: |
168 | 170 | # |
169 | | -# === Get form values as hash |
170 | | -# |
171 | | -# require "cgi" |
| 171 | +# ENV.update( |
| 172 | +# 'REQUEST_METHOD' => 'GET', |
| 173 | +# 'QUERY_STRING' => 'a=111&&b=222&c&d=' |
| 174 | +# ) |
172 | 175 | # cgi = CGI.new |
173 | | -# params = cgi.params |
174 | | -# |
175 | | -# cgi.params is a hash. |
176 | | -# |
177 | | -# cgi.params['new_field_name'] = ["value"] # add new param |
178 | | -# cgi.params['field_name'] = ["new_value"] # change value |
179 | | -# cgi.params.delete('field_name') # delete param |
180 | | -# cgi.params.clear # delete all params |
181 | | -# |
182 | | -# |
183 | | -# === Save form values to file |
184 | | -# |
185 | | -# require "pstore" |
186 | | -# db = PStore.new("query.db") |
187 | | -# db.transaction do |
188 | | -# db["params"] = cgi.params |
| 176 | +# cgi.params.class # => Hash |
| 177 | +# cgi.params # => {"a" => ["111"], "b" => ["222"], "c" => [], "d" => [""]} |
| 178 | +# cgi.params.keys # => ["a", "b", "c", "d"] |
| 179 | +# cgi.params['a'] # => ["111"] # Returns an array. |
| 180 | +# cgi.params['d'] # => [""] # Returns empty string in array if no value. |
| 181 | +# cgi.params['x'] # => [] # Returns empty array if no key. |
| 182 | +# |
| 183 | +# A \CGI instance has these convenience methods: |
| 184 | +# |
| 185 | +# # Convenience method for cgi.params.keys. |
| 186 | +# cgi.keys # => ["a", "b", "c", "d"] |
| 187 | +# # Convenience method for cgi.params[key].first. |
| 188 | +# cgi['a'] # => "111" # Returns string, not array. |
| 189 | +# cgi['d'] # => "" # Returns empty string if no value. |
| 190 | +# cgi['x'] # => "" # Returns empty string if no key. |
| 191 | +# # Convenience method for cgi.params.include?. |
| 192 | +# cgi.include?('a') # => true |
| 193 | +# cgi.include?('x') # => false |
| 194 | +# |
| 195 | +# ==== Save and Restore Form Values |
| 196 | +# |
| 197 | +# This example uses {Pstore}[https://docs.ruby-lang.org/en/3.4/PStore.html] |
| 198 | +# to store and retrieve form values: |
| 199 | +# |
| 200 | +# ENV.update( |
| 201 | +# 'REQUEST_METHOD' => 'GET', |
| 202 | +# 'QUERY_STRING' => 'a=111&&b=222&c&d=' |
| 203 | +# ) |
| 204 | +# cgi = CGI.new |
| 205 | +# require 'pstore' |
| 206 | +# store = PStore.new('params.store') |
| 207 | +# store.transaction do |
| 208 | +# store['params'] = cgi.params |
189 | 209 | # end |
190 | | -# |
191 | | -# |
192 | | -# === Restore form values from file |
193 | | -# |
194 | | -# require "pstore" |
195 | | -# db = PStore.new("query.db") |
196 | | -# db.transaction do |
197 | | -# cgi.params = db["params"] |
| 210 | +# cgi.params.clear # Oops! Lost my params! |
| 211 | +# store.transaction do |
| 212 | +# cgi.params = store['params'] |
198 | 213 | # end |
| 214 | +# cgi.params # => {"a" => ["111"], "b" => ["222"], "c" => [], "d" => [""]} |
199 | 215 | # |
| 216 | +# ==== Get multipart form values |
200 | 217 | # |
201 | | -# === Get multipart form values |
202 | | -# |
203 | | -# require "cgi" |
204 | 218 | # cgi = CGI.new |
205 | 219 | # value = cgi['field_name'] # <== value string for 'field_name' |
206 | 220 | # value.read # <== body of value |
|
212 | 226 | # |
213 | 227 | # === Get cookie values |
214 | 228 | # |
215 | | -# require "cgi" |
216 | 229 | # cgi = CGI.new |
217 | 230 | # values = cgi.cookies['name'] # <== array of 'name' |
218 | 231 | # # if not 'name' included, then return []. |
|
222 | 235 | # |
223 | 236 | # === Get cookie objects |
224 | 237 | # |
225 | | -# require "cgi" |
226 | 238 | # cgi = CGI.new |
227 | 239 | # for name, cookie in cgi.cookies |
228 | 240 | # cookie.expires = Time.now + 30 |
|
231 | 243 | # |
232 | 244 | # cgi.cookies # { "name1" => cookie1, "name2" => cookie2, ... } |
233 | 245 | # |
234 | | -# require "cgi" |
235 | 246 | # cgi = CGI.new |
236 | 247 | # cgi.cookies['name'].expires = Time.now + 30 |
237 | 248 | # cgi.out("cookie" => cgi.cookies['name']) {"string"} |
238 | 249 | # |
239 | 250 | # === Print http header and html string to $DEFAULT_OUTPUT ($>) |
240 | 251 | # |
241 | | -# require "cgi" |
242 | 252 | # cgi = CGI.new("html4") # add HTML generation methods |
243 | 253 | # cgi.out do |
244 | 254 | # cgi.html do |
|
0 commit comments