Trongate Docs
switch to dark modeswitch to dark mode
»
»
The Form Helper

The Form Helper

The Form Helper is a class that has been created to assist with rapid creation of online forms. The following features are available from the Form Helper:

Form Open

Purpose: Generates form open tag for a form that is to be used for HTTP POST requests.

Arguments: Accepts up to three arguments:

  • 1st Argument: $location - a string representing the location that you would like your form to be posted to.
  • 2nd Argument (optional): $attributes - an array containing key value pairs that can be added to the form opening tags.
  • 3rd Argument (optional): $additional_code - a string that can be added onto the end of the form tag.

For Example:

Let's assume that you are building a form for the following website URL:

http://www.your-site.com/members

Let's also assume that you wanted to build a form that is to initiate a POST request that is to be sent to:

http://www.your-site.com/members/submit

To generate form opening tags, we could use the following code inside of a view file:

<?php
echo form_open('members/submit');
?>

The above code will generate the following HTML:

<form action="http://www.your-site.com/members/submit" method="post">

PLEASE NOTE: The result above is made with the assumption that the BASE_URL is set to http://www.your-site.com/

Form Close

Purpose: Generate a simple form close tag.

Example:

<?php
echo form_close();
?>

The above code will generate the following HTML:

</form>

Form Input Helpers

The Trongate framework also comes with a rich assortment of form field helpers. The table below references all of the form field helpers that are available for use:

Method Purpose Arguments
form_label
Purpose: Output a form label.
Output a form label.
  1. $label_text (string) - Form label text
  2. $attributes (array) optional - A (key/value pair) array of attributes
  3. $additional_code (string) optional - A string to be concatinated onto the label tag
form_input
Purpose: Output form input (text) field.
Output form input (text) field.
  1. $name (string) - Form input field name
  2. $value (string) optional - Form field value
  3. $attributes (array) optional - A (key/value pair) array of attributes
  4. $additional_code (string) optional - A string to be concatinated onto the input tag
form_email
Purpose: Output form input (email) field.
Output form input (email) field.
  1. $name (string) - Form email field name
  2. $value (string) optional - Form field value
  3. $attributes (array) optional - A (key/value pair) array of attributes
  4. $additional_code (string) optional - A string to be concatinated onto the input tag
form_password
Purpose: Output form input (password) field.
Output form input (password) field.
  1. $name (string) - Form password field name
  2. $value (string) optional - Form field value
  3. $attributes (array) optional - A (key/value pair) array of attributes
  4. $additional_code (string) optional - A string to be concatinated onto the input tag
form_hidden
Purpose: Output hidden form input field.
Output hidden form input field.
  1. $name (string) - Form field name
  2. $value (string) optional - Form field value
  3. $additional_code (string) optional - A string to be concatinated onto the form hidden tag
form_textarea
Purpose: Output form textarea field.
Output form textarea field.
  1. $name (string) - Form textarea name
  2. $value (string) optional - Form textarea value
  3. $attributes (array) optional - A (key/value pair) array of attributes
  4. $additional_code (string) optional - A string to be concatinated onto the opening tag
form_dropdown
Purpose: Output form select menu.
Output form select menu.
  1. $name (string) - Form field name
  2. $options (array) optional - A (key/value pair) array of selectable options
  3. $selected_key (string) optional - Option key to be marked with 'selected' attribute
  4. $attributes (array) optional - A (key/value pair) array of attributes
  5. $additional_code (string) optional - A string to be concatinated onto the opening tag
form_checkbox
Purpose: Output form checkbox.
Output form checkbox.
  1. $name (string) - Form field name
  2. $value (string) optional - Form field value
  3. $checked (boolean) optional - An attribute for declaring if checkbox state is 'checked'
  4. $attributes (array) optional - A (key/value pair) array of attributes
  5. $additional_code (string) optional - A string to be concatinated onto the checkbox tag
form_radio
Purpose: Output form radio button.
Output form radio button.
  1. $name (string) - Form field name
  2. $value (string) optional - Form field value
  3. $checked (boolean) optional - Determines if radio button has 'selected' state
  4. $attributes (array) optional - A (key/value pair) array of attributes
  5. $additional_code (string) optional - A string to be concatinated onto the radio button tag

Form Buttons

The Form Helper also offers the following two helpers for generating form buttons:

Method Purpose Arguments
form_submit
Purpose: Output form submit button.
Output form submit button.
  1. $name (string) - Button name
  2. $value (string) optional - Button value
  3. $attributes (array) optional - A (key/value pair) array of attributes
  4. $additional_code (string) optional - A string to be concatinated onto the opening tag
form_button
Purpose: Output form button.
Output form button.
  1. $name (string) - Button name
  2. $value (string) optional - Button value
  3. $attributes (array) optional - A (key/value pair) array of attributes
  4. $additional_code (string) optional - A string to be concatinated onto the opening tag

Example

Below is example of some code generation syntax. This is the kind of code that could be added to a view file to generate a form.

<?php
echo form_open_upload('books/submit');
echo form_input('title', '');
echo form_input('author', '');
echo form_textarea('information', '');
echo form_submit('submit', 'Submit');
echo form_close();
?>

Form Open Upload

Purpose: Creates a form opening tag for uploading files. It accepts the same arguments as the Form Open helper, as explained above.

Example:

<?php
echo form_open_upload('members/submit_upload_picture');
?>

The above code will generate the following HTML:

<form action="http://www.website.com/members/submit" method="post" enctype="multipart/form-data">

PLEASE NOTE: The result above is made with the assumption that the BASE_URL is set to http://www.website.com/


HELP & SUPPORT

If you have a question or a comment relating to anything you've see here, please goto the Help Bar.

 
×