###########
Form Helper
###########
The Form Helper file contains functions that assist in working with
forms.
.. contents::
:local:
.. raw:: html
Loading this Helper
===================
This helper is loaded using the following code::
$this->load->helper('form');
Escaping field values
=====================
You may need to use HTML and characters such as quotes within your form
elements. In order to do that safely, you'll need to use
:doc:`common function <../general/common_functions>`
:func:`html_escape()`.
Consider the following example::
$string = 'Here is a string containing "quoted" text.';
Since the above string contains a set of quotes, it will cause the form
to break. The :php:func:`html_escape()` function converts HTML special
characters so that it can be used safely::
.. note:: If you use any of the form helper functions listed on this page,
the form values will be automatically escaped, so there is no need
to call this function. Use it only if you are creating your own
form elements.
Available Functions
===================
The following functions are available:
.. php:function:: form_open([$action = ''[, $attributes = ''[, $hidden = array()]]])
:param string $action: Form action/target URI string
:param array $attributes: HTML attributes
:param array $hidden: An array of hidden fields' definitions
:returns: An HTML form opening tag
:rtype: 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, and
will always add the `accept-charset` attribute based on the charset value in your
config file.
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.
Here's a simple example::
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::
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::
$string = '';
echo form_close($string);
// Would produce:
.. php:function:: set_value($field[, $default = ''[, $html_escape = TRUE]])
:param string $field: Field name
:param string $default: Default value
:param bool $html_escape: Whether to turn off HTML escaping of the value
:returns: Field value
:rtype: string
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. The third (optional) parameter allows you to turn off HTML escaping
of the value, in case you need to use this function in combination with
i.e. :php:func:`form_input()` and avoid double-escaping.
Example::
The above form will show "0" when loaded for the first time.
.. note:: If you've loaded the :doc:`Form Validation Library <../libraries/form_validation>` and
have set a validation rule for the field name in use with this helper, then it will
forward the call to the :doc:`Form Validation Library <../libraries/form_validation>`'s
own ``set_value()`` method. Otherwise, this function looks in ``$_POST`` for the
field value.
.. php:function:: set_select($field[, $value = ''[, $default = FALSE]])
:param string $field: Field name
:param string $value: Value to check for
:param string $default: Whether the value is also a default one
:returns: 'selected' attribute or an empty string
:rtype: string
If you use a