blob: 3ffad7c62e8c4f43eb10d8501eb82245ec0b339e [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
Derek Allard404e35d2007-08-07 01:00:45 +00005<title>CodeIgniter User Guide : Form Helper</title>
adminb0dd10f2006-08-25 17:25:49 +00006
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
Derek Allardb3412372007-10-25 12:15:16 +000013<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000014
15<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
19<meta name='author' content='Rick Ellis' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000020<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000021
22</head>
23<body>
24
25<!-- START NAVIGATION -->
26<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
28<div id="masthead">
29<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30<tr>
Derek Allard60ca9b72007-07-12 19:53:27 +000031<td><h1>CodeIgniter User Guide Version 1.5.4</h1></td>
adminc0d5d522006-10-30 19:40:35 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000033</tr>
34</table>
35</div>
36<!-- END NAVIGATION -->
37
38
39<!-- START BREADCRUMB -->
40<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41<tr>
42<td id="breadcrumb">
Derek Allardd2df9bc2007-04-15 17:41:17 +000043<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000044<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45Form Helper
46</td>
Derek Allardbc030912007-06-24 18:25:29 +000047<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&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
adminb0dd10f2006-08-25 17:25:49 +000048</tr>
49</table>
50<!-- END BREADCRUMB -->
51
52<br clear="all" />
53
54
55<!-- START CONTENT -->
56<div id="content">
57
58
59<h1>Form Helper</h1>
60
61<p>The Form Helper file contains functions that assist in working with forms.</p>
62
63
64<h2>Loading this Helper</h2>
65
66<p>This helper is loaded using the following code:</p>
67<code>$this->load->helper('form');</code>
68
69<p>The following functions are available:</p>
70
71
72
73<h2>form_open()</h2>
74
75<p>Creates an opening form tag with a base URL <strong>built from your config preferences</strong>. It will optionally let you
76add form attributes and hidden input fields.</p>
77
78<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
79in the event your URLs ever change.</p>
80
81<p>Here's a simple example:</p>
82
83<code>echo form_open('email/send');</code>
84
85<p>The above example would create a form that points to your base URL plus the "email/send" URI segments, like this:</p>
86
87<code>&lt;form method="post" action="http:/www.your-site.com/index.php/email/send" /></code>
88
89<h4>Adding Attributes</h4>
90
91<p>Attributes can be added by passing an associative array to the second parameter, like this:</p>
92
93<code>
94$attributes = array('class' => 'email', 'id' => 'myform');<br />
95<br />
96echo form_open('email/send', $attributes);</code>
97
98<p>The above example would create a form similar to this:</p>
99
100<code>&lt;form method="post" action="http:/www.your-site.com/index.php/email/send" &nbsp;class="email" &nbsp;id="myform" /></code>
101
102<h4>Adding Hidden Input Fields</h4>
103
104<p>Hidden fields can be added by passing an associative array to the third parameter, like this:</p>
105
106<code>
107$hidden = array('username' => 'Joe', 'member_id' => '234');<br />
108<br />
109echo form_open('email/send', '', $hidden);</code>
110
111<p>The above example would create a form similar to this:</p>
112
113<code>&lt;form method="post" action="http:/www.your-site.com/index.php/email/send" &nbsp;class="email" &nbsp;id="myform" /><br />
114&lt;input type="hidden" name="username" value="Joe" /><br />
115&lt;input type="hidden" name="member_id" value="234" /></code>
116
117
118<h2>form_open_multipart()</h2>
119
120<p>This function is absolutely identical to the <dfn>form_open()</dfn> tag above except that it adds a multipart attribute,
121which is necessary if you would like to use the form to upload files with.</p>
122
123<h2>form_hidden()</h2>
124
125<p>Lets you generate hidden input fields. You can either submit a name/value string to create one field:</p>
126
127<code>form_hidden('username', 'johndoe');<br />
128<br />
129// Would produce:<br /><br />
130&lt;input type="hidden" name="username" value="johnodoe" /></code>
131
132<p>Or you can submit an associative array to create multiple fields:</p>
133
134<code>$data = array(<br />
135&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'name'&nbsp;&nbsp;=> 'John Doe',<br />
Derek Allard8788d4b2007-08-08 17:04:40 +0000136&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'email' => 'john@example.com',<br />
137&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'url'&nbsp;&nbsp;&nbsp;=> 'http://www.example.com'<br />
adminb0dd10f2006-08-25 17:25:49 +0000138&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
139<br />
140echo form_hidden($data);<br />
141<br />
142// Would produce:<br /><br />
143&lt;input type="hidden" name="name" value="John Doe" /><br />
Derek Allard8788d4b2007-08-08 17:04:40 +0000144&lt;input type="hidden" name="email" value="john@example.com" /><br />
145&lt;input type="hidden" name="url" value="http://www.example.com" /></code>
adminb0dd10f2006-08-25 17:25:49 +0000146
147
148
149
150<h2>form_input()</h2>
151
152<p>Lets you generate a standard text input field. You can minimally pass the field name and value in the first
Derek Allardc6441282007-07-04 23:54:32 +0000153and second parameter:</p>
adminb0dd10f2006-08-25 17:25:49 +0000154
155<code>echo form_input('username', 'johndoe');</code>
156
157<p>Or you can pass an associative array containing any data you wish your form to contain:</p>
158
159<code>$data = array(<br />
160&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'name'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> 'username',<br />
161&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'id'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> 'username',<br />
162&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'value'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> 'johndoe',<br />
163&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'maxlength'&nbsp;&nbsp;&nbsp;=> '100',<br />
164&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'size'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> '50',<br />
165&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'style'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; => 'width:50%',<br />
166&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
167<br />
168echo form_input($data);<br />
169<br />
170// Would produce:<br /><br />
171&lt;input type="text" name="username" id="username" value="johndoe" maxlength="100" size="50" style="width:50%" /></code>
172
admine334c472006-10-21 19:44:22 +0000173<p>If you would like your form to contain some additional data, like JavaScript, you can pass it as a string in the
Derek Allardc6441282007-07-04 23:54:32 +0000174third parameter:</p>
adminb0dd10f2006-08-25 17:25:49 +0000175
176<code>$js = 'onClick="some_function()"';<br />
177<br />
178echo form_input('username', 'johndoe', $js);</code>
179
180<h2>form_password()</h2>
181
admine334c472006-10-21 19:44:22 +0000182<p>This function is identical in all respects to the <dfn>form_input()</dfn> function above
adminb0dd10f2006-08-25 17:25:49 +0000183except that is sets it as a "password" type.</p>
184
185<h2>form_upload()</h2>
186
admine334c472006-10-21 19:44:22 +0000187<p>This function is identical in all respects to the <dfn>form_input()</dfn> function above
adminb0dd10f2006-08-25 17:25:49 +0000188except that is sets it as a "file" type, allowing it to be used to upload files.</p>
189
190<h2>form_textarea()</h2>
191
admine334c472006-10-21 19:44:22 +0000192<p>This function is identical in all respects to the <dfn>form_input()</dfn> function above
adminb0dd10f2006-08-25 17:25:49 +0000193except that it generates a "textarea" type. Note: Instead of the "maxlength" and "size" attributes in the above
194example, you will instead specify "rows" and "cols".</p>
195
196
197<h2>form_dropdown()</h2>
198
199<p>Lets you create a standard drop-down field. The first parameter will contain the name of the field,
200the second parameter will contain an associative array of options, and the third parameter will contain the
Derek Allardc6441282007-07-04 23:54:32 +0000201value you wish to be selected. Example:</p>
adminb0dd10f2006-08-25 17:25:49 +0000202
203<code>$options = array(<br />
204&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'small'&nbsp;&nbsp;=> 'Small Shirt',<br />
205&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'med'&nbsp;&nbsp;&nbsp;&nbsp;=> 'Medium Shirt',<br />
adminb071bb52006-08-26 19:28:37 +0000206&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'large'&nbsp;&nbsp; => 'Large Shirt',<br />
adminb0dd10f2006-08-25 17:25:49 +0000207&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'xlarge' => 'Extra Large Shirt',<br />
208&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
209<br />
210echo form_dropdown('shirts', $options, 'large');<br />
211<br />
212// Would produce:<br /><br />
213
214&lt;select name="shirts"><br />
adminb071bb52006-08-26 19:28:37 +0000215&lt;option value="small">Small Shirt&lt;/option><br />
216&lt;option value="med">Medium Shirt&lt;/option><br />
217&lt;option value="large" selected>Large Shirt&lt;/option><br />
218&lt;option value="xlarge">Extra Large Shirt&lt;/option><br />
adminb0dd10f2006-08-25 17:25:49 +0000219&lt;/select></code>
220
221
admine334c472006-10-21 19:44:22 +0000222<p>If you would like the opening &lt;select> to contain additional data, like JavaScript, you can pass it as a string in the
Derek Allardc6441282007-07-04 23:54:32 +0000223fourth parameter:</p>
adminb0dd10f2006-08-25 17:25:49 +0000224
225<code>$js = 'onChange="some_function()"';<br />
226<br />
227echo form_dropdown('shirts', $options, 'large', $js);</code>
228
229
230<h2>form_checkbox()</h2>
231
Derek Allardc6441282007-07-04 23:54:32 +0000232<p>Lets you generate a checkbox field. Simple example:</p>
adminb0dd10f2006-08-25 17:25:49 +0000233
234
235<code>echo form_checkbox('newsletter', 'accept', TRUE);<br />
236<br />
237// Would produce:<br />
238<br />
239&lt;input type="checkbox" name="newsletter" value="accept" checked="checked" /></code>
240
241<p>The third parameter contains a boolean TRUE/FALSE to determine whether the box should be checked or not.</p>
242
243<p>Similar to the other form functions in this helper, you can also pass an array of attributes to the function:</p>
244
245<code>$data = array(<br />
246&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'name'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> 'newsletter',<br />
247&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'id'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> 'newsletter',<br />
248&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'value'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> 'accept',<br />
249&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'checked'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> TRUE,<br />
250&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'style'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; => 'margin:10px',<br />
251&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
252<br />
253echo form_checkbox($data);<br />
254<br />
255// Would produce:<br /><br />
256&lt;input type="checkbox" name="newsletter" id="newsletter" value="accept" checked="checked" style="margin:10px" /></code>
257
admine334c472006-10-21 19:44:22 +0000258<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
Derek Allardc6441282007-07-04 23:54:32 +0000259fourth parameter:</p>
adminb0dd10f2006-08-25 17:25:49 +0000260
261<code>$js = 'onClick="some_function()"';<br />
262<br />
Derek Allardd4e680a2007-05-02 01:09:23 +0000263 echo form_checkbox('newsletter', 'accept', TRUE, $js)</code>
adminb0dd10f2006-08-25 17:25:49 +0000264
265
266<h2>form_radio()</h2>
267<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>
268
269
270<h2>form_submit()</h2>
271
272<p>Lets you generate a standard submit button. Simple example:</p>
273
274<code>echo form_submit('mysubmit', 'Submit Post!');<br />
275<br />
276// Would produce:<br />
277<br />
278&lt;input type="submit" name="mysubmit" value="Submit Post!" /></code>
279
280<p>Similar to other functions, you can submit an associative array in the first parameter if you prefer to set your own attributes.
281The third parameter lets you add extra data to your form, like JavaScript.</p>
282
283
284<h2>form_close()</h2>
285
286<p>Produces a closing &lt;/form> tag. The only advantage to using this function is it permits you to pass data to it
287which will be added below the tag. For example:</p>
288
289<code>$string = "&lt;/div>&lt;/div>";<br />
290<br />
291echo form_close($string);<br />
292<br />
293// Would produce:<br />
294<br />
295&lt;/form><br />
296&lt;/div>&lt;/div></code>
297
298
299
300
301
302<h2>form_prep()</h2>
303
304<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>
305
306<code>$string = 'Here is a string containing <strong>"quoted"</strong> text.';<br />
307<br />
308&lt;input type="text" name="myform" value="<var>$string</var>" /></code>
309
310<p>Since the above string contains a set of quotes it will cause the form to break.
311The form_prep function converts HTML so that it can be used safely:</p>
312
313<code>&lt;input type="text" name="myform" value="<var>&lt;?php echo form_prep($string); ?></var>" /></code>
314
315<p class="important"><strong>Note:</strong> If you use any of the form helper functions listed in this page the form
316values will be prepped automatically, so there is no need to call this function. Use it only if you are
317creating your own form elements.</p>
318
319
320
321
322</div>
323<!-- END CONTENT -->
324
325
326<div id="footer">
327<p>
328Previous Topic:&nbsp;&nbsp;<a href="file_helper.html">File Helper</a>
329&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
330<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
331<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
332Next Topic:&nbsp;&nbsp;<a href="html_helper.html">HTML Helper</a>
Derek Allardc6441282007-07-04 23:54:32 +0000333</p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000334<p><a href="http://www.codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminb0dd10f2006-08-25 17:25:49 +0000335</div>
336
337</body>
338</html>