1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Form Helper : CodeIgniter User Guide</title>
<style type='text/css' media='all'>@import url('../userguide.css');</style>
<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
<script type="text/javascript" src="../nav/nav.js"></script>
<script type="text/javascript" src="../nav/prototype.lite.js"></script>
<script type="text/javascript" src="../nav/moo.fx.js"></script>
<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />
<meta name='author' content='ExpressionEngine Dev Team' />
<meta name='description' content='CodeIgniter User Guide' />
</head>
<body>
<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>CodeIgniter User Guide Version 1.7</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->
<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="http://codeigniter.com/">CodeIgniter Home</a> ›
<a href="../index.html">User Guide Home</a> ›
Form Helper
</td>
<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
</tr>
</table>
<!-- END BREADCRUMB -->
<br clear="all" />
<!-- START CONTENT -->
<div id="content">
<h1>Form Helper</h1>
<p>The Form Helper file contains functions that assist in working with forms.</p>
<h2>Loading this Helper</h2>
<p>This helper is loaded using the following code:</p>
<code>$this->load->helper('form');</code>
<p>The following functions are available:</p>
<h2>form_open()</h2>
<p>Creates an opening form tag with a base URL <strong>built from your config preferences</strong>. It will optionally let you
add form attributes and hidden input fields.</p>
<p>The main benefit of using this tag rather than hard coding your own HTML is that it permits your site to be more portable
in the event your URLs ever change.</p>
<p>Here's a simple example:</p>
<code>echo form_open('email/send');</code>
<p>The above example would create a form that points to your base URL plus the "email/send" URI segments, like this:</p>
<code><form method="post" action="http:/example.com/index.php/email/send" /></code>
<h4>Adding Attributes</h4>
<p>Attributes can be added by passing an associative array to the second parameter, like this:</p>
<code>
$attributes = array('class' => 'email', 'id' => 'myform');<br />
<br />
echo form_open('email/send', $attributes);</code>
<p>The above example would create a form similar to this:</p>
<code><form method="post" action="http:/example.com/index.php/email/send" class="email" id="myform" /></code>
<h4>Adding Hidden Input Fields</h4>
<p>Hidden fields can be added by passing an associative array to the third parameter, like this:</p>
<code>
$hidden = array('username' => 'Joe', 'member_id' => '234');<br />
<br />
echo form_open('email/send', '', $hidden);</code>
<p>The above example would create a form similar to this:</p>
<code><form method="post" action="http:/example.com/index.php/email/send"><br />
<input type="hidden" name="username" value="Joe" /><br />
<input type="hidden" name="member_id" value="234" /></code>
<h2>form_open_multipart()</h2>
<p>This function is absolutely identical to the <dfn>form_open()</dfn> tag above except that it adds a multipart attribute,
which is necessary if you would like to use the form to upload files with.</p>
<h2>form_hidden()</h2>
<p>Lets you generate hidden input fields. You can either submit a name/value string to create one field:</p>
<code>form_hidden('username', 'johndoe');<br />
<br />
// Would produce:<br /><br />
<input type="hidden" name="username" value="johndoe" /></code>
<p>Or you can submit an associative array to create multiple fields:</p>
<code>$data = array(<br />
'name' => 'John Doe',<br />
'email' => 'john@example.com',<br />
'url' => 'http://example.com'<br />
);<br />
<br />
echo form_hidden($data);<br />
<br />
// Would produce:<br /><br />
<input type="hidden" name="name" value="John Doe" /><br />
<input type="hidden" name="email" value="john@example.com" /><br />
<input type="hidden" name="url" value="http://example.com" /></code>
<h2>form_input()</h2>
<p>Lets you generate a standard text input field. You can minimally pass the field name and value in the first
and second parameter:</p>
<code>echo form_input('username', 'johndoe');</code>
<p>Or you can pass an associative array containing any data you wish your form to contain:</p>
<code>$data = array(<br />
'name' => 'username',<br />
'id' => 'username',<br />
'value' => 'johndoe',<br />
'maxlength' => '100',<br />
'size' => '50',<br />
'style' => 'width:50%',<br />
);<br />
<br />
echo form_input($data);<br />
<br />
// Would produce:<br /><br />
<input type="text" name="username" id="username" value="johndoe" maxlength="100" size="50" style="width:50%" /></code>
<p>If you would like your form to contain some additional data, like Javascript, you can pass it as a string in the
third parameter:</p>
<code>$js = 'onClick="some_function()"';<br />
<br />
echo form_input('username', 'johndoe', $js);</code>
<h2>form_password()</h2>
<p>This function is identical in all respects to the <dfn>form_input()</dfn> function above
except that is sets it as a "password" type.</p>
<h2>form_upload()</h2>
<p>This function is identical in all respects to the <dfn>form_input()</dfn> function above
except that is sets it as a "file" type, allowing it to be used to upload files.</p>
<h2>form_textarea()</h2>
<p>This function is identical in all respects to the <dfn>form_input()</dfn> function above
except that it generates a "textarea" type. Note: Instead of the "maxlength" and "size" attributes in the above
example, you will instead specify "rows" and "cols".</p>
<h2>form_dropdown()</h2>
<p>Lets you create a standard drop-down field. The first parameter will contain the name of the field,
the second parameter will contain an associative array of options, and the third parameter will contain the
value you wish to be selected. You can also pass an array through the third parameter, and CodeIgniter will create a multiple select for you. Example:</p>
<code>$options = array(<br />
'small' => 'Small Shirt',<br />
'med' => 'Medium Shirt',<br />
'large' => 'Large Shirt',<br />
'xlarge' => 'Extra Large Shirt',<br />
);<br />
<br />
$shirts_on_sale = array('small', 'large');<br />
<br />
echo form_dropdown('shirts', $options, 'large');<br />
<br />
// Would produce:<br />
<br />
<select name="shirts"><br />
<option value="small">Small Shirt</option><br />
<option value="med">Medium Shirt</option><br />
<option value="large" selected="selected">Large Shirt</option><br />
<option value="xlarge">Extra Large Shirt</option><br />
</select><br />
<br />
echo form_dropdown('shirts', $options, $shirts_on_sale);<br />
<br />
// Would produce:<br />
<br />
<select name="shirts" multiple="multiple"><br />
<option value="small" selected="selected">Small Shirt</option><br />
<option value="med">Medium Shirt</option><br />
<option value="large" selected="selected">Large Shirt</option><br />
<option value="xlarge">Extra Large Shirt</option><br />
</select></code>
<p>If you would like the opening <select> to contain additional data, like an <kbd>id</kbd> attribute or JavaScript, you can pass it as a string in the
fourth parameter:</p>
<code>$js = 'id="shirts" onChange="some_function();"';<br />
<br />
echo form_dropdown('shirts', $options, 'large', $js);</code>
<p>If the array passed as $options is a multidimensional array, form_dropdown() will produce an <optgroup> with the array key as the label.</p>
<h2>form_fieldset()</h2>
<p>Lets you generate fieldset/legend fields.</p>
<code>echo form_fieldset('Address Information');<br />
echo "<p>fieldset content here</p>\n";<br />
echo form_fieldset_close();
<br />
<br />
// Produces<br />
<fieldset>
<br />
<legend>Address Information</legend>
<br />
<p>form content here</p>
<br />
</fieldset></code>
<p>Similar to other functions, you can submit an associative array in the second parameter if you prefer to set additional attributes. </p>
<p><code>$attributes = array('id' => 'address_info', 'class' => 'address_info');<br />
echo form_fieldset('Address Information', $attributes);<br />
echo "<p>fieldset content here</p>\n";<br />
echo form_fieldset_close(); <br />
<br />
// Produces<br />
<fieldset id="address_info" class="address_info"> <br />
<legend>Address Information</legend> <br />
<p>form content here</p> <br />
</fieldset></code></p>
<h2>form_fieldset_close()</h2>
<p>Produces a closing </fieldset> tag. The only advantage to using this function is it permits you to pass data to it
which will be added below the tag. For example:</p>
<code>$string = "</div></div>";<br />
<br />
echo fieldset_close($string);<br />
<br />
// Would produce:<br />
</fieldset><br />
</div></div></code>
<h2>form_checkbox()</h2>
<p>Lets you generate a checkbox field. Simple example:</p>
<code>echo form_checkbox('newsletter', 'accept', TRUE);<br />
<br />
// Would produce:<br />
<br />
<input type="checkbox" name="newsletter" value="accept" checked="checked" /></code>
<p>The third parameter contains a boolean TRUE/FALSE to determine whether the box should be checked or not.</p>
<p>Similar to the other form functions in this helper, you can also pass an array of attributes to the function:</p>
<code>$data = array(<br />
'name' => 'newsletter',<br />
'id' => 'newsletter',<br />
'value' => 'accept',<br />
'checked' => TRUE,<br />
'style' => 'margin:10px',<br />
);<br />
<br />
echo form_checkbox($data);<br />
<br />
// Would produce:<br /><br />
<input type="checkbox" name="newsletter" id="newsletter" value="accept" checked="checked" style="margin:10px" /></code>
<p>As with other functions, if you would like the tag to contain additional data, like JavaScript, you can pass it as a string in the
fourth parameter:</p>
<code>$js = 'onClick="some_function()"';<br />
<br />
echo form_checkbox('newsletter', 'accept', TRUE, $js)</code>
<h2>form_radio()</h2>
<p>This function is identical in all respects to the <dfn>form_checkbox()</dfn> function above except that is sets it as a "radio" type.</p>
<h2>form_submit()</h2>
<p>Lets you generate a standard submit button. Simple example:</p>
<code>echo form_submit('mysubmit', 'Submit Post!');<br />
<br />
// Would produce:<br />
<br />
<input type="submit" name="mysubmit" value="Submit Post!" /></code>
<p>Similar to other functions, you can submit an associative array in the first parameter if you prefer to set your own attributes.
The third parameter lets you add extra data to your form, like JavaScript.</p>
<h2>form_label()</h2>
<p>Lets you generate a <label>. Simple example:</p>
<code>echo form_label('What is your Name', 'username');<br />
<br />
// Would produce:
<br />
<label for="username">What is your Name</label></code>
<p>Similar to other functions, you can submit an associative array in the third parameter if you prefer to set additional attributes. </p>
<p><code>$attributes = array(<br />
'class' => 'mycustomclass',<br />
'style' => 'color: #000;',<br />
);<br />
echo form_label('What is your Name', 'username', $attributes);<br />
<br />
// Would produce: <br />
<label for="username" class="mycustomclass" style="color: #000;">What is your Name</label></code></p>
<h2>form_reset()</h2>
<p>Lets you generate a standard reset button. Use is identical to <dfn>form_submit()</dfn>.</p>
<h2>form_button()</h2>
<p>Lets you generate a standard button element. You can minimally pass the button name and content in the first and second parameter:</p>
<code>
echo form_button('name','content');<br />
<br />
// Would produce<br />
<button name="name" type="button">Content</button>
</code>
Or you can pass an associative array containing any data you wish your form to contain:
<code>
$data = array(<br />
'name' => 'button',<br />
'id' => 'button',<br />
'value' => 'true',<br />
'type' => 'reset',<br />
'content' => 'Reset'<br />
);<br />
<br />
echo form_button($data);<br />
<br />
// Would produce:<br />
<button name="button" id="button" value="true" type="reset">Reset</button>
</code>
If you would like your form to contain some additional data, like JavaScript, you can pass it as a string in the third parameter:
<code>
$js = 'onClick="some_function()"';<br /><br />
echo form_button('mybutton', 'Click Me', $js);
</code>
<h2>form_close()</h2>
<p>Produces a closing </form> tag. The only advantage to using this function is it permits you to pass data to it
which will be added below the tag. For example:</p>
<code>$string = "</div></div>";<br />
<br />
echo form_close($string);<br />
<br />
// Would produce:<br />
<br />
</form><br />
</div></div></code>
<h2>form_prep()</h2>
<p>Allows you to safely use HTML and characters such as quotes within form elements without breaking out of the form. Consider this example:</p>
<code>$string = 'Here is a string containing <strong>"quoted"</strong> text.';<br />
<br />
<input type="text" name="myform" value="<var>$string</var>" /></code>
<p>Since the above string contains a set of quotes it will cause the form to break.
The form_prep function converts HTML so that it can be used safely:</p>
<code><input type="text" name="myform" value="<var><?php echo form_prep($string); ?></var>" /></code>
<p class="important"><strong>Note:</strong> If you use any of the form helper functions listed in this page the form
values will be prepped automatically, so there is no need to call this function. Use it only if you are
creating your own form elements.</p>
<h2>set_value()</h2>
<p>Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function.
The second (optional) parameter allows you to set a default value for the form. Example:</p>
<code><input type="text" name="quantity" value="<dfn><?php echo set_value('quantity', '0'); ?></dfn>" size="50" /></code>
<p>The above form will show "0" when loaded for the first time.</p>
<h2>set_select()</h2>
<p>If you use a <dfn><select></dfn> menu, this function permits you to display the menu item that was selected. The first parameter
must contain the name of the select menu, the second parameter must contain the value of
each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).</p>
<p>Example:</p>
<code>
<select name="myselect"><br />
<option value="one" <dfn><?php echo set_select('myselect', 'one', TRUE); ?></dfn> >One</option><br />
<option value="two" <dfn><?php echo set_select('myselect', 'two'); ?></dfn> >Two</option><br />
<option value="three" <dfn><?php echo set_select('myselect', 'three'); ?></dfn> >Three</option><br />
</select>
</code>
<h2>set_checkbox()</h2>
<p>Permits you to display a checkbox in the state it was submitted. The first parameter
must contain the name of the checkbox, the second parameter must contain its value, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE). Example:</p>
<code><input type="checkbox" name="mycheck" value="1" <dfn><?php echo set_checkbox('mycheck', '1'); ?></dfn> /><br />
<input type="checkbox" name="mycheck" value="2" <dfn><?php echo set_checkbox('mycheck', '2'); ?></dfn> /></code>
<h2>set_radio()</h2>
<p>Permits you to display radio buttons in the state they were submitted. This function is identical to the <strong>set_checkbox()</strong> function above.</p>
<code><input type="radio" name="myradio" value="1" <dfn><?php echo set_radio('myradio', '1', TRUE); ?></dfn> /><br />
<input type="radio" name="myradio" value="2" <dfn><?php echo set_radio('myradio', '2'); ?></dfn> /></code>
</div>
<!-- END CONTENT -->
<div id="footer">
<p>
Previous Topic: <a href="file_helper.html">File Helper</a>
·
<a href="#top">Top of Page</a> ·
<a href="../index.html">User Guide Home</a> ·
Next Topic: <a href="html_helper.html">HTML Helper</a>
</p>
<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2008 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
</div>
</body>
</html>
|