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 = '', $id = null) {
21 return '<input type="'.$type.'"'.
22 ($name !== null ?
' name="'.htmlspecialchars($name).'"' : '').
23 ($id !== null ?
' id="'.htmlspecialchars($id).'"'
24 : ($name !== null ?
' id="'.htmlspecialchars($name).'"' : '')).
25 ($value !== null ?
' value="'.htmlspecialchars($value).'"' : '').
26 $attributes . " />\n";
30 * Password input field
32 function addPwField($name , $value = null) {
33 return addInputField('password', $name , $value);
40 function addCheckBox($name, $checked = false, $value = null) {
41 return addInputField('checkbox', $name, $value,
42 ($checked ?
' checked="checked"' : ''));
48 function addRadioBox($name, $checked = false, $value = null) {
49 return addInputField('radio', $name, $value,
50 ($checked ?
' checked="checked"' : ''), $name . $value);
54 * A hidden form field.
56 function addHidden($name, $value) {
57 return addInputField('hidden', $name, $value);
63 function addInput($name, $value = '', $size = 0, $maxlength = 0) {
67 $attr.= ' size="'.(int)$size.'"';
70 $attr.= ' maxlength="'.(int)$maxlength .'"';
73 return addInputField('text', $name, $value, $attr);
78 * Function to create a selectlist from an array.
80 * name: html name attribute
81 * values: array ( key => value ) -> <option value="key">value</option>
82 * default: the key that will be selected
83 * usekeys: use the keys of the array as option value or not
85 function addSelect($name, $values, $default = null, $usekeys = false)
88 if(count($values) == 1) {
89 $k = key($values); $v = array_pop($values);
90 return addHidden($name, ($usekeys ?
$k:$v)).
91 htmlspecialchars($v) . "\n";
94 $ret = '<select name="'.htmlspecialchars($name) . "\">\n";
95 foreach ($values as $k => $v) {
96 if(!$usekeys) $k = $v;
97 $ret .= '<option value="' .
98 htmlspecialchars( $k ) . '"' .
99 (($default == $k) ?
' selected="selected"' : '') .
100 '>' . htmlspecialchars($v) ."</option>\n";
102 $ret .= "</select>\n";
108 * Form submission button
109 * Note the switched value/name parameters!
111 function addSubmit($value, $name = null) {
112 return addInputField('submit', $name, $value);
115 * Form reset button, $value = caption
117 function addReset($value) {
118 return addInputField('reset', null, $value);
122 * Textarea form element.
124 function addTextArea($name, $text = '', $cols = 40, $rows = 10, $attr = '') {
125 return '<textarea name="'.htmlspecialchars($name).'" '.
126 'rows="'.(int)$rows .'" cols="'.(int)$cols.'" '.
127 $attr . '>'.htmlspecialchars($text) ."</textarea>\n";
131 * Make a <form> start-tag.
133 function addForm($action, $method = 'post', $name = '', $enctype = '', $charset = '')
136 $name = ' name="'.$name.'"';
139 $enctype = ' enctype="'.$enctype.'"';
142 $charset = ' accept-charset="'.htmlspecialchars($charset).'"';
145 return '<form action="'. $action .'" method="'. $method .'"'.
146 $enctype . $name . $charset . ">\n";