Form Helper
The Form Helper file contains functions that assist in working with forms.
Loading this Helper
This helper is loaded using the following code:
$this->load->helper('form');
The following functions are available:
form_open()
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.
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:
<form method="post" action="http:/www.your-site.com/index.php/email/send" />
< 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:
<form method="post" action="http:/www.your-site.com/index.php/email/send" />
Adding Attributes
Attributes can be added by passing an associative array to the second parameter, like this:
$attributes = array('class' => 'email', 'id' => 'myform');
echo form_open('email/send', $attributes);
The above example would create a form similar to this:
<form method="post" action="http:/www.your-site.com/index.php/email/send" class="email" id="myform" />
Adding Hidden Input Fields
Hidden fields can be added by passing an associative array to the third parameter, like this:
$hidden = array('username' => 'Joe', 'member_id' => '234');
echo form_open('email/send', '', $hidden);
The above example would create a form similar to this:
<form method="post" action="http:/www.your-site.com/index.php/email/send" class="email" id="myform" />
<input type="hidden" name="username" value="Joe" />
<input type="hidden" name="member_id" value="234" />
form_open_multipart()
This function is absolutely identical to the form_open() tag above except that it adds a multipart attribute, which is necessary if you would like to use the form to upload files with.
form_hidden()
Lets you generate hidden input fields. You can either submit a name/value string to create one field:
form_hidden('username', 'johndoe');
// Would produce:
<input type="hidden" name="username" value="johnodoe" />
Or you can submit an associative array to create multiple fields:
$data = array(
'name' => 'John Doe',
'email' => 'john@example.com',
'url' => 'http://www.example.com'
);
echo form_hidden($data);
// Would produce:
<input type="hidden" name="name" value="John Doe" />
<input type="hidden" name="email" value="john@example.com" />
<input type="hidden" name="url" value="http://www.example.com" />
form_input()
Lets you generate a standard text input field. You can minimally pass the field name and value in the first and second parameter:
echo form_input('username', 'johndoe');
Or you can pass an associative array containing any data you wish your form to contain:
$data = array(
&nbs