From c3d8e6a78ea47e9133ed426fef4b0e4650e64d3d Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Mon, 17 Dec 2007 13:50:46 +0000 Subject: Added form_reset() function to form helper. --- system/helpers/form_helper.php | 421 +----------------------------------- user_guide/changelog.html | 2 +- user_guide/helpers/form_helper.html | 339 +---------------------------- 3 files changed, 3 insertions(+), 759 deletions(-) diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 17022a3b3..a23d4c6d2 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -1,420 +1 @@ -config->site_url($action).'"'; - - if ( ! isset($attributes['method'])) - { - $form .= ' method="post"'; - } - - if (is_array($attributes) AND count($attributes) > 0) - { - foreach ($attributes as $key => $val) - { - $form .= ' '.$key.'="'.$val.'"'; - } - } - - $form .= '>'; - - if (is_array($hidden) AND count($hidden > 0)) - { - $form .= form_hidden($hidden); - } - - return $form; -} - -// ------------------------------------------------------------------------ - -/** - * Form Declaration - Multipart type - * - * Creates the opening portion of the form, but with "multipart/form-data". - * - * @access public - * @param string the URI segments of the form destination - * @param array a key/value pair of attributes - * @param array a key/value pair hidden data - * @return string - */ -function form_open_multipart($action, $attributes = array(), $hidden = array()) -{ - $attributes['enctype'] = 'multipart/form-data'; - return form_open($action, $attributes, $hidden); -} - -// ------------------------------------------------------------------------ - -/** - * Hidden Input Field - * - * Generates hidden fields. You can pass a simple key/value string or an associative - * array with multiple values. - * - * @access public - * @param mixed - * @param string - * @return string - */ -function form_hidden($name, $value = '') -{ - if ( ! is_array($name)) - { - return ''; - } - - $form = ''; - foreach ($name as $name => $value) - { - $form .= ''; - } - - return $form; -} - -// ------------------------------------------------------------------------ - -/** - * Text Input Field - * - * @access public - * @param mixed - * @param string - * @param string - * @return string - */ -function form_input($data = '', $value = '', $extra = '') -{ - $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50'); - - return "\n"; -} - -// ------------------------------------------------------------------------ - -/** - * Password Field - * - * Identical to the input function but adds the "password" type - * - * @access public - * @param mixed - * @param string - * @param string - * @return string - */ -function form_password($data = '', $value = '', $extra = '') -{ - if ( ! is_array($data)) - { - $data = array('name' => $data); - } - - $data['type'] = 'password'; - return form_input($data, $value, $extra); -} - -// ------------------------------------------------------------------------ - -/** - * Upload Field - * - * Identical to the input function but adds the "file" type - * - * @access public - * @param mixed - * @param string - * @param string - * @return string - */ -function form_upload($data = '', $value = '', $extra = '') -{ - if ( ! is_array($data)) - { - $data = array('name' => $data); - } - - $data['type'] = 'file'; - return form_input($data, $value, $extra); -} - -// ------------------------------------------------------------------------ - -/** - * Textarea field - * - * @access public - * @param mixed - * @param string - * @param string - * @return string - */ -function form_textarea($data = '', $value = '', $extra = '') -{ - $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12'); - - if ( ! is_array($data) OR ! isset($data['value'])) - { - $val = $value; - } - else - { - $val = $data['value']; - unset($data['value']); // textareas don't use the value attribute - } - - return "\n"; -} - -// ------------------------------------------------------------------------ - -/** - * Drop-down Menu - * - * @access public - * @param string - * @param array - * @param string - * @param string - * @return string - */ -function form_dropdown($name = '', $options = array(), $selected = '', $extra = '') -{ - if ($extra != '') $extra = ' '.$extra; - - $form = ''; - - return $form; -} - -// ------------------------------------------------------------------------ - -/** - * Checkbox Field - * - * @access public - * @param mixed - * @param string - * @param bool - * @param string - * @return string - */ -function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '') -{ - $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); - - if (is_array($data) AND array_key_exists('checked', $data)) - { - $checked = $data['checked']; - - if ($checked == FALSE) - { - unset($data['checked']); - } - else - { - $data['checked'] = 'checked'; - } - } - - if ($checked == TRUE) - $defaults['checked'] = 'checked'; - else - unset($defaults['checked']); - - return "\n"; -} - -// ------------------------------------------------------------------------ - -/** - * Radio Button - * - * @access public - * @param mixed - * @param string - * @param bool - * @param string - * @return string - */ -function form_radio($data = '', $value = '', $checked = TRUE, $extra = '') -{ - if ( ! is_array($data)) - { - $data = array('name' => $data); - } - - $data['type'] = 'radio'; - return form_checkbox($data, $value, $checked, $extra); -} - -// ------------------------------------------------------------------------ - -/** - * Submit Button - * - * @access public - * @param mixed - * @param string - * @param string - * @return string - */ -function form_submit($data = '', $value = '', $extra = '') -{ - $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); - - return "\n"; -} - -// ------------------------------------------------------------------------ - -/** - * Form Close Tag - * - * @access public - * @param string - * @return string - */ -function form_close($extra = '') -{ - return "\n".$extra; -} - -// ------------------------------------------------------------------------ - -/** - * Form Prep - * - * Formats text so that it can be safely placed in a form field in the event it has HTML tags. - * - * @access public - * @param string - * @return string - */ -function form_prep($str = '') -{ - if ($str === '') - { - return ''; - } - - $temp = '__TEMP_AMPERSANDS__'; - - // Replace entities to temporary markers so that - // htmlspecialchars won't mess them up - $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str); - $str = preg_replace("/&(\w+);/", "$temp\\1;", $str); - - $str = htmlspecialchars($str); - - // In case htmlspecialchars misses these. - $str = str_replace(array("'", '"'), array("'", """), $str); - - // Decode the temp markers back to entities - $str = preg_replace("/$temp(\d+);/","&#\\1;",$str); - $str = preg_replace("/$temp(\w+);/","&\\1;",$str); - - return $str; -} - -// ------------------------------------------------------------------------ - -/** - * Parse the form attributes - * - * Helper function used by some of the form helpers - * - * @access private - * @param array - * @param array - * @return string - */ -function parse_form_attributes($attributes, $default) -{ - if (is_array($attributes)) - { - foreach ($default as $key => $val) - { - if (isset($attributes[$key])) - { - $default[$key] = $attributes[$key]; - unset($attributes[$key]); - } - } - - if (count($attributes) > 0) - { - $default = array_merge($default, $attributes); - } - } - - $att = ''; - foreach ($default as $key => $val) - { - if ($key == 'value') - { - $val = form_prep($val); - } - - $att .= $key . '="' . $val . '" '; - } - - return $att; -} - -?> \ No newline at end of file +config->site_url($action).'"'; if ( ! isset($attributes['method'])) { $form .= ' method="post"'; } if (is_array($attributes) AND count($attributes) > 0) { foreach ($attributes as $key => $val) { $form .= ' '.$key.'="'.$val.'"'; } } $form .= '>'; if (is_array($hidden) AND count($hidden > 0)) { $form .= form_hidden($hidden); } return $form; } // ------------------------------------------------------------------------ /** * Form Declaration - Multipart type * * Creates the opening portion of the form, but with "multipart/form-data". * * @access public * @param string the URI segments of the form destination * @param array a key/value pair of attributes * @param array a key/value pair hidden data * @return string */ function form_open_multipart($action, $attributes = array(), $hidden = array()) { $attributes['enctype'] = 'multipart/form-data'; return form_open($action, $attributes, $hidden); } // ------------------------------------------------------------------------ /** * Hidden Input Field * * Generates hidden fields. You can pass a simple key/value string or an associative * array with multiple values. * * @access public * @param mixed * @param string * @return string */ function form_hidden($name, $value = '') { if ( ! is_array($name)) { return ''; } $form = ''; foreach ($name as $name => $value) { $form .= ''; } return $form; } // ------------------------------------------------------------------------ /** * Text Input Field * * @access public * @param mixed * @param string * @param string * @return string */ function form_input($data = '', $value = '', $extra = '') { $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value, 'maxlength' => '500', 'size' => '50'); return "\n"; } // ------------------------------------------------------------------------ /** * Password Field * * Identical to the input function but adds the "password" type * * @access public * @param mixed * @param string * @param string * @return string */ function form_password($data = '', $value = '', $extra = '') { if ( ! is_array($data)) { $data = array('name' => $data); } $data['type'] = 'password'; return form_input($data, $value, $extra); } // ------------------------------------------------------------------------ /** * Upload Field * * Identical to the input function but adds the "file" type * * @access public * @param mixed * @param string * @param string * @return string */ function form_upload($data = '', $value = '', $extra = '') { if ( ! is_array($data)) { $data = array('name' => $data); } $data['type'] = 'file'; return form_input($data, $value, $extra); } // ------------------------------------------------------------------------ /** * Textarea field * * @access public * @param mixed * @param string * @param string * @return string */ function form_textarea($data = '', $value = '', $extra = '') { $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12'); if ( ! is_array($data) OR ! isset($data['value'])) { $val = $value; } else { $val = $data['value']; unset($data['value']); // textareas don't use the value attribute } return "\n"; } // ------------------------------------------------------------------------ /** * Drop-down Menu * * @access public * @param string * @param array * @param string * @param string * @return string */ function form_dropdown($name = '', $options = array(), $selected = '', $extra = '') { if ($extra != '') $extra = ' '.$extra; $form = ''; return $form; } // ------------------------------------------------------------------------ /** * Checkbox Field * * @access public * @param mixed * @param string * @param bool * @param string * @return string */ function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '') { $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); if (is_array($data) AND array_key_exists('checked', $data)) { $checked = $data['checked']; if ($checked == FALSE) { unset($data['checked']); } else { $data['checked'] = 'checked'; } } if ($checked == TRUE) $defaults['checked'] = 'checked'; else unset($defaults['checked']); return "\n"; } // ------------------------------------------------------------------------ /** * Radio Button * * @access public * @param mixed * @param string * @param bool * @param string * @return string */ function form_radio($data = '', $value = '', $checked = TRUE, $extra = '') { if ( ! is_array($data)) { $data = array('name' => $data); } $data['type'] = 'radio'; return form_checkbox($data, $value, $checked, $extra); } // ------------------------------------------------------------------------ /** * Submit Button * * @access public * @param mixed * @param string * @param string * @return string */ function form_submit($data = '', $value = '', $extra = '') { $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); return "\n"; } // ------------------------------------------------------------------------ /** * Reset Button * * @access public * @param mixed * @param string * @param string * @return string */ function form_reset($data = '', $value = '', $extra = '') { $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value); return "\n"; } // ------------------------------------------------------------------------ /** * Form Close Tag * * @access public * @param string * @return string */ function form_close($extra = '') { return "\n".$extra; } // ------------------------------------------------------------------------ /** * Form Prep * * Formats text so that it can be safely placed in a form field in the event it has HTML tags. * * @access public * @param string * @return string */ function form_prep($str = '') { if ($str === '') { return ''; } $temp = '__TEMP_AMPERSANDS__'; // Replace entities to temporary markers so that // htmlspecialchars won't mess them up $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str); $str = preg_replace("/&(\w+);/", "$temp\\1;", $str); $str = htmlspecialchars($str); // In case htmlspecialchars misses these. $str = str_replace(array("'", '"'), array("'", """), $str); // Decode the temp markers back to entities $str = preg_replace("/$temp(\d+);/","&#\\1;",$str); $str = preg_replace("/$temp(\w+);/","&\\1;",$str); return $str; } // ------------------------------------------------------------------------ /** * Parse the form attributes * * Helper function used by some of the form helpers * * @access private * @param array * @param array * @return string */ function parse_form_attributes($attributes, $default) { if (is_array($attributes)) { foreach ($default as $key => $val) { if (isset($attributes[$key])) { $default[$key] = $attributes[$key]; unset($attributes[$key]); } } if (count($attributes) > 0) { $default = array_merge($default, $attributes); } } $att = ''; foreach ($default as $key => $val) { if ($key == 'value') { $val = form_prep($val); } $att .= $key . '="' . $val . '" '; } return $att; } ?> \ No newline at end of file diff --git a/user_guide/changelog.html b/user_guide/changelog.html index 6d94d4ace..3301a9d8e 100644 --- a/user_guide/changelog.html +++ b/user_guide/changelog.html @@ -1 +1 @@ - CodeIgniter User Guide : Change Log

CodeIgniter User Guide Version 1.5.4


Change Log

Version 1.5.5

Release Date: -- still in development

Version 1.5.4

Release Date: July 12, 2007

Version 1.5.3

Release Date: April 15, 2007

Version 1.5.2

Release Date: February 13, 2007

Version 1.5.1

Release Date: November 23, 2006

Version 1.5.0.1

Release Date: October 31, 2006

Version 1.5.0

Release Date: October 30, 2006

Version 1.4.1

Release Date: September 21, 2006

Version 1.4.0

Release Date: September 17, 2006

Version 1.3.3

Release Date: June 1, 2006

Version 1.3.2

Release Date: April 17, 2006

Version 1.3.1

Release Date: April 11, 2006

Version 1.3

Release Date: April 3, 2006

Version 1.2

Release Date: March 21, 2006

Version Beta 1.1

Release Date: March 10, 2006

Version Beta 1.0

Release Date: February 28, 2006

First publicly released version.

\ No newline at end of file + CodeIgniter User Guide : Change Log

CodeIgniter User Guide Version 1.5.4


Change Log

Version 1.5.5

Release Date: -- still in development

Version 1.5.4

Release Date: July 12, 2007

Version 1.5.3

Release Date: April 15, 2007

Version 1.5.2

Release Date: February 13, 2007

Version 1.5.1

Release Date: November 23, 2006

Version 1.5.0.1

Release Date: October 31, 2006

Version 1.5.0

Release Date: October 30, 2006

Version 1.4.1

Release Date: September 21, 2006

Version 1.4.0

Release Date: September 17, 2006

Version 1.3.3

Release Date: June 1, 2006

Version 1.3.2

Release Date: April 17, 2006

Version 1.3.1

Release Date: April 11, 2006

Version 1.3

Release Date: April 3, 2006

Version 1.2

Release Date: March 21, 2006

Version Beta 1.1

Release Date: March 10, 2006

Version Beta 1.0

Release Date: February 28, 2006

First publicly released version.

\ No newline at end of file diff --git a/user_guide/helpers/form_helper.html b/user_guide/helpers/form_helper.html index 3ffad7c62..3cf6f3a6b 100644 --- a/user_guide/helpers/form_helper.html +++ b/user_guide/helpers/form_helper.html @@ -1,338 +1 @@ - - - - -CodeIgniter User Guide : Form Helper - - - - - - - - - - - - - - - - - - - - - - -
- - - - - -

CodeIgniter User Guide Version 1.5.4

-
- - - - - - - - - -
- - -
- - - -
- - -

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" /> - -

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(
-              'name'        => 'username',
-              'id'          => 'username',
-              'value'       => 'johndoe',
-              'maxlength'   => '100',
-              'size'        => '50',
-              'style'       => 'width:50%',
-            );
-
-echo form_input($data);
-
-// Would produce:

-<input type="text" name="username" id="username" value="johndoe" maxlength="100" size="50" style="width:50%" />
- -

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 is sets it as a "password" type.

- -

form_upload()

- -

This function is identical in all respects to the form_input() function above -except that is sets it as a "file" 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. Example:

- -$options = array(
-                  'small'  => 'Small Shirt',
-                  'med'    => 'Medium Shirt',
-                  'large'   => 'Large Shirt',
-                  'xlarge' => 'Extra Large Shirt',
-                );
-
-echo form_dropdown('shirts', $options, 'large');
-
-// Would produce:

- -<select name="shirts">
-<option value="small">Small Shirt</option>
-<option value="med">Medium Shirt</option>
-<option value="large" selected>Large Shirt</option>
-<option value="xlarge">Extra Large Shirt</option>
-</select>
- - -

If you would like the opening <select> to contain additional data, like JavaScript, you can pass it as a string in the -fourth parameter:

- -$js = 'onChange="some_function()"';
-
-echo form_dropdown('shirts', $options, 'large', $js);
- - -

form_checkbox()

- -

Lets you generate a checkbox field. Simple example:

- - -echo form_checkbox('newsletter', 'accept', TRUE);
-
-// Would produce:
-
-<input type="checkbox" name="newsletter" value="accept" checked="checked" />
- -

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:

-<input type="checkbox" name="newsletter" id="newsletter" value="accept" checked="checked" style="margin:10px" />
- -

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 is sets it as a "radio" type.

- - -

form_submit()

- -

Lets you generate a standard submit button. Simple example:

- -echo form_submit('mysubmit', 'Submit Post!');
-
-// Would produce:
-
-<input type="submit" name="mysubmit" value="Submit Post!" />
- -

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_close()

- -

Produces a closing </form> 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 = "</div></div>";
-
-echo form_close($string);
-
-// Would produce:
-
-</form>
-</div></div>
- - - - - -

form_prep()

- -

Allows you to safely use HTML and characters such as quotes within form elements without breaking out of the form. Consider this example:

- -$string = 'Here is a string containing "quoted" text.';
-
-<input type="text" name="myform" value="$string" />
- -

Since the above string contains a set of quotes it will cause the form to break. -The form_prep function converts HTML so that it can be used safely:

- -<input type="text" name="myform" value="<?php echo form_prep($string); ?>" /> - -

Note: If you use any of the form helper functions listed in this page the form -values will be prepped automatically, so there is no need to call this function. Use it only if you are -creating your own form elements.

- - - - -
- - - - - - - \ No newline at end of file + CodeIgniter User Guide : Form Helper

CodeIgniter User Guide Version 1.5.4


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" />

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(
              'name'        => 'username',
              'id'          => 'username',
              'value'       => 'johndoe',
              'maxlength'   => '100',
              'size'        => '50',
              'style'       => 'width:50%',
            );

echo form_input($data);

// Would produce:

<input type="text" name="username" id="username" value="johndoe" maxlength="100" size="50" style="width:50%" />

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 is sets it as a "password" type.

form_upload()

This function is identical in all respects to the form_input() function above except that is sets it as a "file" 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. Example:

$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

echo form_dropdown('shirts', $options, 'large');

// Would produce:

<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected>Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>

If you would like the opening <select> to contain additional data, like JavaScript, you can pass it as a string in the fourth parameter:

$js = 'onChange="some_function()"';

echo form_dropdown('shirts', $options, 'large', $js);

form_checkbox()

Lets you generate a checkbox field. Simple example:

echo form_checkbox('newsletter', 'accept', TRUE);

// Would produce:

<input type="checkbox" name="newsletter" value="accept" checked="checked" />

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:

<input type="checkbox" name="newsletter" id="newsletter" value="accept" checked="checked" style="margin:10px" />

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 is sets it as a "radio" type.

form_submit()

Lets you generate a standard submit button. Simple example:

echo form_submit('mysubmit', 'Submit Post!');

// Would produce:

<input type="submit" name="mysubmit" value="Submit Post!" />

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_reset()

Lets you generate a standard reset button. Use is identical to form_submit().

form_close()

Produces a closing </form> 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 = "</div></div>";

echo form_close($string);

// Would produce:

</form>
</div></div>

form_prep()

Allows you to safely use HTML and characters such as quotes within form elements without breaking out of the form. Consider this example:

$string = 'Here is a string containing "quoted" text.';

<input type="text" name="myform" value="$string" />

Since the above string contains a set of quotes it will cause the form to break. The form_prep function converts HTML so that it can be used safely:

<input type="text" name="myform" value="<?php echo form_prep($string); ?>" />

Note: If you use any of the form helper functions listed in this page the form values will be prepped automatically, so there is no need to call this function. Use it only if you are creating your own form elements.

\ No newline at end of file -- cgit v1.2.3-24-g4f1b