Form Helper

The Form Helper file contains functions that assist in working withforms.

Loading this Helper

This helper is loaded using the following code:

  1. helper('form');

Escaping field values

You may need to use HTML and characters such as quotes within your formelements. In order to do that safely, you’ll need to usecommon functionesc().

Consider the following example:

  1. $string = 'Here is a string containing "quoted" text.';
  2.  
  3. <input type="text" name="myfield" value="<?= $string; ?>" />

Since the above string contains a set of quotes, it will cause the formto break. The esc() function converts HTML specialcharacters so that it can be used safely:

  1. <input type="text" name="myfield" value="<?= esc($string); ?>" />

Note

If you use any of the form helper functions listed on this page,and you pass values as an associative array,the form values will be automatically escaped, so there is no needto call this function. Use it only if you are creating your ownform elements, which you would pass as strings.

Available Functions

The following functions are available:

  • formopen([$action = ''[, $attributes = ''[, $hidden = []_]]])

Parameters:

  • $action (string) – Form action/target URI string
  • $attributes (mixed) – HTML attributes, as an array or escaped string
  • $hidden (array) – An array of hidden fields’ definitionsReturns:An HTML form opening tagReturn type:string

Creates an opening form tag with a base URL built from your config preferences.It will optionally let you add form attributes and hidden input fields, andwill always add the accept-charset attribute based on the charset value in yourconfig file.

The main benefit of using this tag rather than hard coding your own HTML is thatit permits your site to be more portable in the event your URLs ever change.

Here’s a simple example:

  1. echo form_open('email/send');

The above example would create a form that points to your base URL plus the“email/send” URI segments, like this:

  1. <form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send">

Adding Attributes

Attributes can be added by passing an associative array to the secondparameter, like this:

  1. $attributes = ['class' => 'email', 'id' => 'myform'];echo form_open('email/send', $attributes);

Alternatively, you can specify the second parameter as a string:

  1. echo form_open('email/send', 'class="email" id="myform"');

The above examples would create a form similar to this:

  1. <form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send&#34; class="email" id="myform">

If CSRF filter is turned on form_open() will generate CSRF field at the beginning of the form. You can specify ID of this field by passing csrf_id as one of the $attribute array:

form_open(‘/u/sign-up’, [‘csrf_id’ => ‘my-id’]);

will return:

<form action=”/u/sign-up” method=”post” accept-charset=”utf-8”><input type=”hidden” id=”my-id” name=”csrf_field” value=”964ede6e0ae8a680f7b8eab69136717d” />

Adding Hidden Input Fields

Hidden fields can be added by passing an associative array to thethird parameter, like this:

  1. $hidden = ['username' => 'Joe', 'member_id' => '234'];echo form_open('email/send', '', $hidden);

You can skip the second parameter by passing any false value to it.

The above example would create a form similar to this:

  1. <form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send"&gt; <input type="hidden" name="username" value="Joe" /> <input type="hidden" name="member_id" value="234" />
  • formopen_multipart([$action = ''[, $attributes = ''[, $hidden = []_]]])

Parameters:

  • $action (string) – Form action/target URI string
  • $attributes (mixed) – HTML attributes, as an array or escaped string
  • $hidden (array) – An array of hidden fields’ definitionsReturns:An HTML multipart form opening tagReturn type:string

This function is identical to form_open() above,except that it adds a multipart attribute, which is necessary if youwould like to use the form to upload files with.

  • formhidden($name[, $value = ''_])

Parameters:

  • $name (string) – Field name
  • $value (string) – Field valueReturns:An HTML hidden input field tagReturn type:string

Lets you generate hidden input fields. You can either submit aname/value string to create one field:

  1. form_hidden('username', 'johndoe');
  2. // Would produce: <input type="hidden" name="username" value="johndoe" />

… or you can submit an associative array to create multiple fields:

  1. $data = [
  2. 'name' => 'John Doe',
  3. 'email' => 'john@example.com',
  4. 'url' => 'http://example.com'
  5. ];
  6.  
  7. echo form_hidden($data);
  8.  
  9. /*
  10. Would produce:
  11. <input type="hidden" name="name" value="John Doe" />
  12. <input type="hidden" name="email" value="john@example.com" />
  13. <input type="hidden" name="url" value="http://example.com" />
  14. */

You can also pass an associative array to the value field:

  1. $data = [
  2. 'name' => 'John Doe',
  3. 'email' => 'john@example.com',
  4. 'url' => 'http://example.com'
  5. ];
  6.  
  7. echo form_hidden('my_array', $data);
  8.  
  9. /*
  10. Would produce:
  11.  
  12. <input type="hidden" name="my_array[name]" value="John Doe" />
  13. <input type="hidden" name="my_array[email]" value="john@example.com" />
  14. <input type="hidden" name="my_array[url]" value="http://example.com" />
  15. */

If you want to create hidden input fields with extra attributes:

  1. $data = [
  2. 'type' => 'hidden',
  3. 'name' => 'email',
  4. 'id' => 'hiddenemail',
  5. 'value' => 'john@example.com',
  6. 'class' => 'hiddenemail'
  7. ];
  8.  
  9. echo form_input($data);
  10.  
  11. /*
  12. Would produce:
  13.  
  14. <input type="hidden" name="email" value="john@example.com" id="hiddenemail" class="hiddenemail" />
  15. */
  • forminput([$data = ''[, $value = ''[, $extra = ''[, $type = 'text'_]]]])

Parameters:

  • $data (array) – Field attributes data
  • $value (string) – Field value
  • $extra (mixed) – Extra attributes to be added to the tag either as an array or a literal string
  • $type (string) – The type of input field. i.e. ‘text’, ‘email’, ‘number’, etc.Returns:An HTML text input field tagReturn type:string

Lets you generate a standard text input field. You can minimally passthe field name and value in the first and second parameter:

  1. echo form_input('username', 'johndoe');

Or you can pass an associative array containing any data you wish yourform to contain:

  1. $data = [
  2. 'name' => 'username',
  3. 'id' => 'username',
  4. 'value' => 'johndoe',
  5. 'maxlength' => '100',
  6. 'size' => '50',
  7. 'style' => 'width:50%'
  8. ];
  9.  
  10. echo form_input($data);
  11.  
  12. /*
  13. Would produce:
  14.  
  15. <input type="text" name="username" value="johndoe" id="username" maxlength="100" size="50" style="width:50%" />
  16. */

If you would like your form to contain some additional data, likeJavaScript, you can pass it as a string in the third parameter:

  1. $js = 'onClick="some_function()"';
  2. echo form_input('username', 'johndoe', $js);

Or you can pass it as an array:

  1. $js = ['onClick' => 'some_function();'];
  2. echo form_input('username', 'johndoe', $js);

To support the expanded range of HTML5 input fields, you can pass an input type in as the fourth parameter:

  1. echo form_input('email', 'joe@example.com', ['placeholder' => 'Email Address...'], 'email');
  2.  
  3. /*
  4. Would produce:
  5.  
  6. <input type="email" name="email" value="joe@example.com" placeholder="Email Address..." />
  7. */
  • formpassword([$data = ''[, $value = ''[, $extra = ''_]]])

Parameters:

  • $data (array) – Field attributes data
  • $value (string) – Field value
  • $extra (mixed) – Extra attributes to be added to the tag either as an array or a literal stringReturns:An HTML password input field tagReturn type:string

This function is identical in all respects to the form_input()function above except that it uses the “password” input type.

  • formupload([$data = ''[, $value = ''[, $extra = ''_]]])

Parameters:

  • $data (array) – Field attributes data
  • $value (string) – Field value
  • $extra (mixed) – Extra attributes to be added to the tag either as an array or a literal stringReturns:An HTML file upload input field tagReturn type:string

This function is identical in all respects to the form_input()function above except that it uses the “file” input type, allowing it tobe used to upload files.

  • formtextarea([$data = ''[, $value = ''[, $extra = ''_]]])

Parameters:

  • $data (array) – Field attributes data
  • $value (string) – Field value
  • $extra (mixed) – Extra attributes to be added to the tag either as an array or a literal stringReturns:An HTML textarea tagReturn type:string

This function is identical in all respects to the form_input()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.

  • formdropdown([$name = ''[, $options = [][, $selected = [][, $extra = ''_]]]])

Parameters:

  • $name (string) – Field name
  • $options (array) – An associative array of options to be listed
  • $selected (array) – List of fields to mark with the selected attribute
  • $extra (mixed) – Extra attributes to be added to the tag either as an array or a literal stringReturns:An HTML dropdown select field tagReturn type:string

Lets you create a standard drop-down field. The first parameter willcontain the name of the field, the second parameter will contain anassociative array of options, and the third parameter will contain thevalue you wish to be selected. You can also pass an array of multipleitems through the third parameter, and the helper will create amultiple select for you.

Example:

  1. $options = [
  2. 'small' => 'Small Shirt',
  3. 'med' => 'Medium Shirt',
  4. 'large' => 'Large Shirt',
  5. 'xlarge' => 'Extra Large Shirt',
  6. ];
  7.  
  8. $shirts_on_sale = ['small', 'large'];
  9. echo form_dropdown('shirts', $options, 'large');
  10.  
  11. /*
  12. Would produce:
  13.  
  14. <select name="shirts">
  15. <option value="small">Small Shirt</option>
  16. <option value="med">Medium Shirt</option>
  17. <option value="large" selected="selected">Large Shirt</option>
  18. <option value="xlarge">Extra Large Shirt</option>
  19. </select>
  20. */
  21.  
  22. echo form_dropdown('shirts', $options, $shirts_on_sale);
  23.  
  24. /*
  25. Would produce:
  26.  
  27. <select name="shirts" multiple="multiple">
  28. <option value="small" selected="selected">Small Shirt</option>
  29. <option value="med">Medium Shirt</option>
  30. <option value="large" selected="selected">Large Shirt</option>
  31. <option value="xlarge">Extra Large Shirt</option>
  32. </select>
  33. */

If you would like the opening

  • formmultiselect([$name = ''[, $options = [][, $selected = [][, $extra = ''_]]]])

Parameters:

  • $name (string) – Field name
  • $options (array) – An associative array of options to be listed
  • $selected (array) – List of fields to mark with the selected attribute
  • $extra (mixed) – Extra attributes to be added to the tag either as an array or a literal stringReturns:An HTML dropdown multiselect field tagReturn type:string

Lets you create a standard multiselect field. The first parameter willcontain the name of the field, the second parameter will contain anassociative array of options, and the third parameter will contain thevalue or values you wish to be selected.

The parameter usage is identical to using form_dropdown() above,except of course that the name of the field will need to use POST arraysyntax, e.g. foo[].

  • formfieldset([$legendtext = ''[, $attributes = []]])

Parameters:

  • $legend_text (string) – Text to put in the tag
  • $attributes (array) – Attributes to be set on the
    tagReturns:An HTML fieldset opening tagReturn type:string

Lets you generate fieldset/legend fields.

Example:

  1. echo form_fieldset('Address Information');
  2. echo "<p>fieldset content here</p>\n";
  3. echo form_fieldset_close();
  4.  
  5. /*
  6. Produces:
  7.  
  8. <fieldset>
  9. <legend>Address Information</legend>
  10. <p>form content here</p>
  11. </fieldset>
  12. */

Similar to other functions, you can submit an associative array in thesecond parameter if you prefer to set additional attributes:

  1. $attributes = [
  2. 'id' => 'address_info',
  3. 'class' => 'address_info'
  4. ];
  5.  
  6. echo form_fieldset('Address Information', $attributes);
  7. echo "<p>fieldset content here</p>\n";
  8. echo form_fieldset_close();
  9.  
  10. /*
  11. Produces:
  12.  
  13. <fieldset id="address_info" class="address_info">
  14. <legend>Address Information</legend>
  15. <p>form content here</p>
  16. </fieldset>
  17. */

  • formfieldset_close([$extra = ''_])

Parameters:

  • $extra (string) – Anything to append after the closing tag, _as is_Returns:An HTML fieldset closing tagReturn type:string

Produces a closing tag. The only advantage to using thisfunction is it permits you to pass data to it which will be added belowthe tag. For example

  1. $string = '</div></div>';
  2. echo form_fieldset_close($string);
  3. // Would produce: </fieldset></div></div>
  • formcheckbox([$data = ''[, $value = ''[, $checked = FALSE[, $extra = ''_]]]])

Parameters:

  • $data (array) – Field attributes data
  • $value (string) – Field value
  • $checked (bool) – Whether to mark the checkbox as being checked
  • $extra (mixed) – Extra attributes to be added to the tag either as an array or a literal stringReturns:An HTML checkbox input tagReturn type:string

Lets you generate a checkbox field. Simple example:

  1. echo form_checkbox('newsletter', 'accept', TRUE);
  2. // Would produce: <input type="checkbox" name="newsletter" value="accept" checked="checked" />

The third parameter contains a boolean TRUE/FALSE to determine whetherthe box should be checked or not.

Similar to the other form functions in this helper, you can also pass anarray of attributes to the function:

  1. $data = [
  2. 'name' => 'newsletter',
  3. 'id' => 'newsletter',
  4. 'value' => 'accept',
  5. 'checked' => TRUE,
  6. 'style' => 'margin:10px'
  7. ];
  8.  
  9. echo form_checkbox($data);
  10. // Would produce: <input type="checkbox" name="newsletter" id="newsletter" value="accept" checked="checked" style="margin:10px" />

Also as with other functions, if you would like the tag to containadditional data like JavaScript, you can pass it as a string in thefourth parameter:

  1. $js = 'onClick="some_function()"';
  2. echo form_checkbox('newsletter', 'accept', TRUE, $js);

Or you can pass it as an array:

  1. $js = ['onClick' => 'some_function();'];
  2. echo form_checkbox('newsletter', 'accept', TRUE, $js);
  • formradio([$data = ''[, $value = ''[, $checked = FALSE[, $extra = ''_]]]])

Parameters:

  • $data (array) – Field attributes data
  • $value (string) – Field value
  • $checked (bool) – Whether to mark the radio button as being checked
  • $extra (mixed) – Extra attributes to be added to the tag either as an array or a literal stringReturns:An HTML radio input tagReturn type:string

This function is identical in all respects to the form_checkbox()function above except that it uses the “radio” input type.

  • formlabel([$labeltext = ''[, $id = ''[, $attributes = []]]])

Parameters:

  • $label_text (string) – Text to put in the
  • $id (string) – ID of the form element that we’re making a label for
  • $attributes (string) – HTML attributesReturns:An HTML field label tagReturn type:string

Lets you generate a

  1. echo form_label('What is your Name', 'username');
  2. // Would produce: <label for="username">What is your Name</label>

Similar to other functions, you can submit an associative array in thethird parameter if you prefer to set additional attributes.

Example:

  1. $attributes = [
  2. 'class' => 'mycustomclass',
  3. 'style' => 'color: #000;'
  4. ];
  5.  
  6. echo form_label('What is your Name', 'username', $attributes);
  7. // Would produce: <label for="username" class="mycustomclass" style="color: #000;">What is your Name</label>

  • formsubmit([$data = ''[, $value = ''[, $extra = ''_]]])

Parameters:

  • $data (string) – Button name
  • $value (string) – Button value
  • $extra (mixed) – Extra attributes to be added to the tag either as an array or a literal stringReturns:An HTML input submit tagReturn type:string

Lets you generate a standard submit button. Simple example:

  1. echo form_submit('mysubmit', 'Submit Post!');
  2. // Would produce: <input type="submit" name="mysubmit" value="Submit Post!" />

Similar to other functions, you can submit an associative array in thefirst parameter if you prefer to set your own attributes. The thirdparameter lets you add extra data to your form, like JavaScript.

  • formreset([$data = ''[, $value = ''[, $extra = ''_]]])

Parameters:

  • $data (string) – Button name
  • $value (string) – Button value
  • $extra (mixed) – Extra attributes to be added to the tag either as an array or a literal stringReturns:An HTML input reset button tagReturn type:string

Lets you generate a standard reset button. Use is identical toform_submit().

  • formbutton([$data = ''[, $content = ''[, $extra = ''_]]])

Parameters:

  • $data (string) – Button name
  • $content (string) – Button label
  • $extra (mixed) – Extra attributes to be added to the tag either as an array or a literal stringReturns:An HTML button tagReturn type:string

Lets you generate a standard button element. You can minimally pass thebutton name and content in the first and second parameter:

  1. echo form_button('name','content');
  2. // Would produce: <button name="name" type="button">Content</button>

Or you can pass an associative array containing any data you wish yourform to contain:

  1. $data = [
  2. 'name' => 'button',
  3. 'id' => 'button',
  4. 'value' => 'true',
  5. 'type' => 'reset',
  6. 'content' => 'Reset'
  7. ];
  8.  
  9. echo form_button($data);
  10. // Would produce: <button name="button" id="button" value="true" type="reset">Reset</button>

If you would like your form to contain some additional data, likeJavaScript, you can pass it as a string in the third parameter:

  1. $js = 'onClick="some_function()"';
  2. echo form_button('mybutton', 'Click Me', $js);
  • formclose([$extra = ''_])

Parameters:

  • $extra (string) – Anything to append after the closing tag, _as is_Returns:An HTML form closing tagReturn type:string

Produces a closing tag. The only advantage to using thisfunction is it permits you to pass data to it which will be added belowthe tag. For example:

  1. $string = '</div></div>';
  2. echo form_close($string);
  3. // Would produce: </form> </div></div>
  • setvalue($field[, $default = ''[, $htmlescape = TRUE]])

Parameters:

  • $field (string) – Field name
  • $default (string) – Default value
  • $html_escape (bool) – Whether to turn off HTML escaping of the valueReturns:Field valueReturn type:string

Permits you to set the value of an input form or textarea. You mustsupply the field name via the first parameter of the function. Thesecond (optional) parameter allows you to set a default value for theform. The third (optional) parameter allows you to turn off HTML escapingof the value, in case you need to use this function in combination withi.e. form_input() and avoid double-escaping.

Example:

  1. <input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />

The above form will show “0” when loaded for the first time.

  • setselect($field[, $value = ''[, $default = FALSE_]])

Parameters:

  • $field (string) – Field name
  • $value (string) – Value to check for
  • $default (string) – Whether the value is also a default oneReturns:‘selected’ attribute or an empty stringReturn type:string

If you use a

  • setcheckbox($field[, $value = ''[, $default = FALSE_]])

Parameters:

  • $field (string) – Field name
  • $value (string) – Value to check for
  • $default (string) – Whether the value is also a default oneReturns:‘checked’ attribute or an empty stringReturn type:string

Permits you to display a checkbox in the state it was submitted.

The first parameter must contain the name of the checkbox, the secondparameter must contain its value, and the third (optional) parameterlets you set an item as the default (use boolean TRUE/FALSE).

Example:

  1. <input type="checkbox" name="mycheck" value="1" <?php echo set_checkbox('mycheck', '1'); ?> />
  2. <input type="checkbox" name="mycheck" value="2" <?php echo set_checkbox('mycheck', '2'); ?> />
  • setradio($field[, $value = ''[, $default = FALSE_]])

Parameters:

  • $field (string) – Field name
  • $value (string) – Value to check for
  • $default (string) – Whether the value is also a default oneReturns:‘checked’ attribute or an empty stringReturn type:string

Permits you to display radio buttons in the state they were submitted.This function is identical to the set_checkbox() function above.

Example:

  1. <input type="radio" name="myradio" value="1" <?php echo set_radio('myradio', '1', TRUE); ?> />
  2. <input type="radio" name="myradio" value="2" <?php echo set_radio('myradio', '2'); ?> />

Note

If you are using the Form Validation class, you must always specifya rule for your field, even if empty, in order for the set*()functions to work. This is because if a Form Validation object isdefined, the control for set*() is handed over to a method of theclass instead of the generic helper function.