###########
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 :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:
.. 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:
.. function:: set_value($field[, $default = ''])
:param string $field: Field name
:param string $default: Default 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.
Example::
The above form will show "0" when loaded for the first time.
.. note:: Only use this function with raw HTML fields, as it
internally calls :func:`html_escape()` and combining its
usage with other form helper functions will result in
double HTML encoding!
.. 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