Source for file forms.php

Documentation is available at forms.php

  1. <?php
  2.  
  3. /**
  4.  * forms.php - html form functions
  5.  *
  6.  * Functions to build HTML forms in a safe and consistent manner.
  7.  * All name, value attributes are htmlentitied.
  8.  *
  9.  * @link http://www.section508.gov/ Section 508
  10.  * @link http://www.w3.org/WAI/ Web Accessibility Initiative (WAI)
  11.  * @link http://www.w3.org/TR/html4/ W3.org HTML 4.01 form specs
  12.  * @copyright &copy; 2004-2006 The SquirrelMail Project Team
  13.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  14.  * @version $Id: forms.php,v 1.3.2.13 2006/04/14 22:27:07 jervfors Exp $
  15.  * @package squirrelmail
  16.  * @subpackage forms
  17.  * @since 1.4.3 and 1.5.1
  18.  */
  19.  
  20. /**
  21.  * Helper function to create form fields, not to be called directly,
  22.  * only by other functions below.
  23.  */
  24. function addInputField($type, $name = null, $value = null, $attributes = '') {
  25.     return '<input type="'.$type.'"'.
  26.         ($name  !== null ? ' name="'.htmlspecialchars($name).'"'   : '').
  27.         ($value !== null ? ' value="'.htmlspecialchars($value).'"' : '').
  28.         $attributes . " />\n";
  29. }
  30.  
  31. /**
  32.  * Password input field
  33.  */
  34. function addPwField($name , $value = null) {
  35.     return addInputField('password', $name , $value);
  36. }
  37.  
  38.  
  39. /**
  40.  * Form checkbox
  41.  */
  42. function addCheckBox($name, $checked = false, $value = null) {
  43.     return addInputField('checkbox', $name, $value,
  44.         ($checked ? ' checked="checked"' : ''));
  45. }
  46.  
  47. /**
  48.  * Form radio box
  49.  */
  50. function addRadioBox($name, $checked = false, $value = null) {
  51.     return addInputField('radio', $name, $value,
  52.         ($checked ? ' checked="checked"' : ''));
  53. }
  54.  
  55. /**
  56.  * A hidden form field.
  57.  */
  58. function addHidden($name, $value) {
  59.     return addInputField('hidden', $name, $value);
  60. }
  61.  
  62. /**
  63.  * An input textbox.
  64.  */
  65. function addInput($name, $value = '', $size = 0, $maxlength = 0) {
  66.  
  67.     $attr = '';
  68.     if ($size) {
  69.         $attr.= ' size="'.(int)$size.'"';
  70.     }
  71.     if ($maxlength) {
  72.         $attr.= ' maxlength="'.(int)$maxlength .'"';
  73.     }
  74.  
  75.     return addInputField('text', $name, $value, $attr);
  76. }
  77.  
  78.  
  79. /**
  80.  * Function to create a selectlist from an array.
  81.  * Usage:
  82.  * name: html name attribute
  83.  * values: array ( key => value )  ->     <option value="key">value</option>
  84.  * default: the key that will be selected
  85.  * usekeys: use the keys of the array as option value or not
  86.  */
  87. function addSelect($name, $values, $default = null, $usekeys = false)
  88. {
  89.     // only one element
  90.     if(count($values) == 1) {
  91.         $k = key($values); $v = array_pop($values);
  92.         return addHidden($name, ($usekeys ? $k:$v)).
  93.             htmlspecialchars($v) . "\n";
  94.     }
  95.  
  96.     $ret = '<select name="'.htmlspecialchars($name) . "\">\n";
  97.     foreach ($values as $k => $v) {
  98.         if(!$usekeys) $k = $v;
  99.         $ret .= '<option value="' .
  100.             htmlspecialchars( $k ) . '"' .
  101.             (($default == $k) ? ' selected="selected"' : '') .
  102.             '>' . htmlspecialchars($v) ."</option>\n";
  103.     }
  104.     $ret .= "</select>\n";
  105.  
  106.     return $ret;
  107. }
  108.  
  109. /**
  110.  * Form submission button
  111.  * Note the switched value/name parameters!
  112.  */
  113. function addSubmit($value, $name = null) {
  114.     return addInputField('submit', $name, $value);
  115. }
  116. /**
  117.  * Form reset button, $value = caption
  118.  */
  119. function addReset($value) {
  120.     return addInputField('reset', null, $value);
  121. }
  122.  
  123. /**
  124.  * Textarea form element.
  125.  */
  126. function addTextArea($name, $text = '', $cols = 40, $rows = 10, $attr = '') {
  127.     return '<textarea name="'.htmlspecialchars($name).'" '.
  128.         'rows="'.(int)$rows .'" cols="'.(int)$cols.'" '.
  129.         $attr . '>'.htmlspecialchars($text) ."</textarea>\n";
  130. }
  131.  
  132. /**
  133.  * Make a <form> start-tag.
  134.  */
  135. function addForm($action, $method = 'post', $name = '', $enctype = '', $charset = '')
  136. {
  137.     if($name) {
  138.         $name = ' name="'.$name.'"';
  139.     }
  140.     if($enctype) {
  141.         $enctype = ' enctype="'.$enctype.'"';
  142.     }
  143.     if($charset) {
  144.         $charset = ' accept-charset="'.htmlspecialchars($charset).'"';
  145.     }
  146.  
  147.     return '<form action="'. $action .'" method="'. $method .'"'.
  148.         $enctype . $name . $charset . ">\n";
  149. }
  150.  
  151. ?>

Documentation generated on Sat, 07 Oct 2006 16:31:21 +0300 by phpDocumentor 1.3.0RC6