Form Helper¶
The Form Helper file contains functions that assist in working with forms.
Available Functions¶
The following functions are available:
-
form_open
($action = '', $attributes = '')¶ -
Parameters: - $action (string) – Form action/target URI string
- $attributes (array) – HTML attributes
Returns: An HTML form opening tag
Return type: string
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://example.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);
Alternatively, you can specify the second parameter as a string:
echo form_open('email/send', 'class="email" id="myform"');
The above examples would create a form similar to this:
<form method="post" action="http://example.com/index.php/email/send" class="email" id="myform">
-
form_media
($action = '', $attributes = array())¶ -
Parameters: - $action (string) – Form action/target URI string
- $attributes (array) – HTML attributes
Returns: An HTML multipart form opening tag
Return type: string
This function is absolutely identical to
form_open()
above, except that it adds a multipart attribute, which is necessary if you would like to use the form to upload files with.
-
form_input
($data = '')¶ -
Parameters: - $data (array) – Field attributes data
Returns: An HTML text input field tag
Return type: string
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( 'name' => 'username', 'id' => 'username', 'value' => 'johndoe', 'maxlength' => '100', 'size' => '50', 'style' => 'width:50%' ); echo form_input($data); /* Would produce: <input type="text" name="username" value="johndoe" id="username" maxlength="100" size="50" style="width:50%" /> */
-
form_textarea
($data = '')¶ -
Parameters: - $data (array) – Field attributes data
Returns: An HTML textarea tag
Return 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.
-
form_select
($name = '', $options = array())¶ -
Parameters: - $name (string) – Field name
- $options (array) – An associative array of options to be listed
Returns: An HTML dropdown select field tag
Return type: string
Lets you create a standard drop-down field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options.
Example:
$options = array( 'small' => 'Small Shirt', 'med' => 'Medium Shirt', 'large' => 'Large Shirt', 'xlarge' => 'Extra Large Shirt', ); echo form_dropdown('shirts', $options); /* Would produce: <select name="shirts"> <option value="small">Small Shirt</option> <option value="med">Medium Shirt</option> <option value="xlarge">Extra Large Shirt</option> </select> */
-
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 string
Returns: An HTML button tag
Return type: string
Lets you generate a standard button element. You can minimally pass the button name and content in the first and second parameter:
echo form_button('name','content'); // Would produce: <button name="name" type="button">Content</button>
Or you can pass an associative array containing any data you wish your form to contain:
$data = array( 'name' => 'button', 'id' => 'button', 'value' => 'true', 'type' => 'reset', 'content' => 'Reset' ); echo form_button($data); // Would produce: <button name="button" id="button" value="true" type="reset">Reset</button>
-
form_close
()¶ -
Returns: An HTML form closing tag
Return type: string
Produces a closing </form> tag. For example:
echo form_close(); // Would produce: </form>