be4709d98ab3c80edd2d51b510e0e3207c4575e8
5 * Copyright (c) 2004-2005 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
17 * Helper function to create form fields, not to be called directly,
18 * only by other functions below.
20 function addInputField($type, $name = null, $value = null, $attributes = '') {
21 return '<input type="'.$type.'"'.
22 ($name !== null ?
' name="'.htmlspecialchars($name).'"' : '').
23 ($value !== null ?
' value="'.htmlspecialchars($value).'"' : '').
24 $attributes . " />\n";
28 * Password input field
30 function addPwField($name , $value = null) {
31 return addInputField('password', $name , $value);
38 function addCheckBox($name, $checked = false, $value = null) {
39 return addInputField('checkbox', $name, $value,
40 ($checked ?
' checked="checked"' : ''));
46 function addRadioBox($name, $checked = false, $value = null) {
47 return addInputField('radio', $name, $value,
48 ($checked ?
' checked="checked"' : ''));
52 * A hidden form field.
54 function addHidden($name, $value) {
55 return addInputField('hidden', $name, $value);
61 function addInput($name, $value = '', $size = 0, $maxlength = 0) {
65 $attr.= ' size="'.(int)$size.'"';
68 $attr.= ' maxlength="'.(int)$maxlength .'"';
71 return addInputField('text', $name, $value, $attr);
76 * Function to create a selectlist from an array.
78 * name: html name attribute
79 * values: array ( key => value ) -> <option value="key">value</option>
80 * default: the key that will be selected
81 * usekeys: use the keys of the array as option value or not
83 function addSelect($name, $values, $default = null, $usekeys = false)
86 if(count($values) == 1) {
87 $k = key($values); $v = array_pop($values);
88 return addHidden($name, ($usekeys ?
$k:$v)).
89 htmlspecialchars($v) . "\n";
92 $ret = '<select name="'.htmlspecialchars($name) . "\">\n";
93 foreach ($values as $k => $v) {
94 if(!$usekeys) $k = $v;
95 $ret .= '<option value="' .
96 htmlspecialchars( $k ) . '"' .
97 (($default == $k) ?
' selected="selected"' : '') .
98 '>' . htmlspecialchars($v) ."</option>\n";
100 $ret .= "</select>\n";
106 * Form submission button
107 * Note the switched value/name parameters!
109 function addSubmit($value, $name = null) {
110 return addInputField('submit', $name, $value);
113 * Form reset button, $value = caption
115 function addReset($value) {
116 return addInputField('reset', null, $value);
120 * Textarea form element.
122 function addTextArea($name, $text = '', $cols = 40, $rows = 10, $attr = '') {
123 return '<textarea name="'.htmlspecialchars($name).'" '.
124 'rows="'.(int)$rows .'" cols="'.(int)$cols.'" '.
125 $attr . '>'.htmlspecialchars($text) ."</textarea>\n";
129 * Make a <form> start-tag.
131 function addForm($action, $method = 'post', $name = '', $enctype = '', $charset = '')
134 $name = ' name="'.$name.'"';
137 $enctype = ' enctype="'.$enctype.'"';
140 $charset = ' accept-charset="'.htmlspecialchars($charset).'"';
143 return '<form action="'. $action .'" method="'. $method .'"'.
144 $enctype . $name . $charset . ">\n";