5 * Copyright (c) 2004 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * Functions to build HTML forms in a safe and consistent manner.
9 * All name, value attributes are htmlentitied.
12 * @package squirrelmail
16 * Helper function to create form fields, not to be called directly,
17 * only by other functions below.
19 function addInputField($type, $name = null, $value = null, $attributes = '') {
20 return '<input type="'.$type.'"'.
21 ($name !== null ?
' name="'.htmlentities($name).'"' : '').
22 ($value !== null ?
' value="'.htmlentities($value).'"' : '').
27 * Password input field
29 function addPwField($name , $value = null) {
30 return addInputField('password', $name , $value);
37 function addCheckBox($name, $checked = false, $value='') {
38 return addInputField('checkbox', $name, $value,
39 ($checked ?
' checked' : ''));
45 function addRadioBox($name, $checked = false, $value='') {
46 return addInputField('radio', $name, $value,
47 ($checked ?
' checked' : ''));
51 * A hidden form field.
53 function addHidden($name, $value) {
54 return addInputField('hidden', $name, $value);
60 function addInput($name, $value = '', $size = 0, $maxlength = 0) {
64 $attr.= ' size="'.(int)$size.'"';
67 $attr.= ' maxlength="'.(int)$maxlength .'"';
70 return addInputField('text', $name, $value, $attr);
75 * Function to create a selectlist from an array.
77 * name: html name attribute
78 * values: array ( key => value ) -> <option value="key">value
79 * default: the key that will be selected
80 * usekeys: use the keys of the array as option value or not
82 function addSelect($name, $values, $default = null, $usekeys = false)
85 if(count($values) == 1) {
86 $k = key($values); $v = array_pop($values);
87 return addHidden($name, ($usekeys ?
$k:$v)).
88 htmlentities($v) . "\n";
91 $ret = '<select name="'.htmlentities($name) . "\">\n";
92 foreach ($values as $k => $v) {
93 if(!$usekeys) $k = $v;
94 $ret .= '<option value="' .
95 htmlentities( $k ) . '"' .
96 (($default == $k) ?
' selected':'') .
97 '>' . htmlentities($v) ."</option>\n";
99 $ret .= "</select>\n";
105 * Form submission button
106 * Note the switched value/name parameters!
108 function addSubmit($value, $name = null) {
109 return addInputField('submit', $name, $value);
112 * Form reset button, $value = caption
114 function addReset($value) {
115 return addInputField('reset', null, $value);
119 * Textarea form element.
121 function addTextArea($name, $text = '', $cols = 40, $rows = 10, $attr = '') {
122 return '<textarea name="'.htmlentities($name).'" '.
123 'rows="'.(int)$rows .'" cols="'.(int)$cols.'"'.
124 $attr . '">'.htmlentities($text) ."</textarea>\n";
128 * Make a <form> start-tag.
130 function addForm($action, $method = 'POST', $name = '', $enctype = '', $charset = '')
133 $name = ' name="'.$name.'"';
136 $enctype = ' enctype="'.$enctype.'"';
139 $charset = ' accept-charset="'.htmlspecialchars($charset).'"';
142 return '<form action="'. $action .'" method="'. $method .'"'.
143 $enctype . $name . $charset . "\">\n";