########### Form Helper ########### The Form Helper file contains functions that assist in working with forms. .. contents:: Page Contents 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, and will always add the attribute accept-charset 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 ::
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 :: 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_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: Or you can submit an associative array to create multiple fields :: $data = array(                'name'  => 'John Doe',                'email' => 'john@example.com',                'url'   => 'http://example.com'              ); echo form_hidden($data); /* Would produce: */ 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(                'name'        => 'username',                'id'          => 'username',                'value'       => 'johndoe',                'maxlength'   => '100',                'size'        => '50',                'style'       => 'width:50%',              ); echo form_input($data); /* Would produce: */ If you would like your form to contain some additional data, like Javascript, you can pass it as a string in the third parameter :: $js = 'onClick="some_function()"'; echo form_input('username', 'johndoe', $js); form_password() =============== This function is identical in all respects to the `form_input()` function above except that it uses the "password" input type. form_upload() ============= This function is identical in all respects to the `form_input()` function above except that it uses the "file" input type, allowing it to be used to upload files. form_textarea() =============== 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_dropdown() =============== 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, and the third parameter will contain the value you wish to be selected. You can also pass an array of multiple items through the third parameter, and CodeIgniter will create a multiple select for you. Example :: $options = array(                    'small'  => 'Small Shirt',                    'med'    => 'Medium Shirt',                    'large'   => 'Large Shirt',                    'xlarge' => 'Extra Large Shirt',                  ); $shirts_on_sale = array('small', 'large'); echo form_dropdown('shirts', $options, 'large'); /* Would produce: */ echo form_dropdown('shirts', $options, $shirts_on_sale); /* Would produce: */ If you would like the opening The third parameter contains a boolean TRUE/FALSE to determine whether the box should be checked or not. Similar to the other form functions in this helper, you can also pass an array of attributes to the function :: $data = array(      'name'        => 'newsletter',      'id'          => 'newsletter',      'value'       => 'accept',      'checked'     => TRUE,      'style'       => 'margin:10px',      ); echo form_checkbox($data); // Would produce: As with other functions, if you would like the tag to contain additional data, like JavaScript, you can pass it as a string in the fourth parameter :: $js = 'onClick="some_function()"'; echo form_checkbox('newsletter', 'accept', TRUE, $js) form_radio() ============ This function is identical in all respects to the `form_checkbox()` function above except that it uses the "radio" input type. form_submit() ============= Lets you generate a standard submit button. Simple example :: echo form_submit('mysubmit', 'Submit Post!'); // Would produce: Similar to other functions, you can submit an associative array in the first parameter if you prefer to set your own attributes. The third parameter lets you add extra data to your form, like JavaScript. form_label() ============ Lets you generate a