📄 form.php
字号:
<?php
//
// +----------------------------------------------------------------------+
// | PHP version 4.0 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2001 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Stig Bakken <ssb@fast.no> |
// | Urs Gehrig <urs@circle.ch> |
// +----------------------------------------------------------------------+
//
// $Id: Form.php,v 1.12.2.2 2001/11/13 01:26:45 ssb Exp $
//
// HTML form utility functions.
//
if (!defined('HTML_FORM_TEXT_SIZE')) {
define('HTML_FORM_TEXT_SIZE', 20);
}
if (!defined('HTML_FORM_MAX_FILE_SIZE')) {
define('HTML_FORM_MAX_FILE_SIZE', 1048576); // 1 MB
}
if (!defined('HTML_FORM_PASSWD_SIZE')) {
define('HTML_FORM_PASSWD_SIZE', 8);
}
class HTML_Form
{
// {{{ properties
/** ACTION attribute of <FORM> tag */
var $action;
/** METHOD attribute of <FORM> tag */
var $method;
/** NAME attribute of <FORM> tag */
var $name;
/** an array of entries for this form */
var $fields;
/** DB_storage object, if tied to one */
var $storageObject;
/** <FORM ENCODING=""> attribute value */
var $encoding;
/** TARGET attribute of <FORM> tag */
var $target;
/** ENCTYPE attribute of <FORM> tag */
var $enctype;
// }}}
// {{{ constructor
function HTML_Form($action, $method = 'GET', $name = '', $target = '', $enctype = '')
{
$this->action = $action;
$this->method = $method;
$this->name = $name;
$this->fields = array();
$this->encoding = '';
$this->target = $target;
$this->enctype = $enctype;
}
// }}}
// {{{ addText()
function addText($name, $title, $default = '',
$size = HTML_FORM_TEXT_SIZE, $maxlength = '')
{
$this->fields[] = array("text", $name, $title, $default, $size, $maxlength);
}
// }}}
// {{{ addPassword()
function addPassword($name, $title, $default, $size = HTML_FORM_PASSWD_SIZE)
{
$this->fields[] = array("password", $name, $title, $default, $size);
}
// }}}
// {{{ addCheckbox()
function addCheckbox($name, $title, $default)
{
$this->fields[] = array("checkbox", $name, $title, $default);
}
// }}}
// {{{ addTextarea()
function addTextarea($name, $title, $default,
$width = HTML_FORM_TEXTAREA_WT,
$height = HTML_FORM_TEXTAREA_HT, $maxlength = '')
{
$this->fields[] = array("textarea", $name, $title, $default, $width, $height, $maxlength);
}
// }}}
// {{{ addSubmit()
function addSubmit($name = "submit", $title = "Submit Changes")
{
$this->fields[] = array("submit", $name, $title);
}
// }}}
// {{{ addReset()
function addReset($title = "Discard Changes")
{
$this->fields[] = array("reset", $title);
}
// }}}
// {{{ addSelect()
function addSelect($name, $title, $entries, $default = '', $size = 1,
$blank = '', $multiple = false, $attribs = '')
{
$this->fields[] = array("select", $name, $title, $entries, $default,
$size, $blank, $multiple, $attribs);
}
// }}}
// {{{ addRadio()
function addRadio($name, $title, $value, $default = false)
{
$this->fields[] = array("radio", $name, $title, $value, $default);
}
// }}}
// {{{ addImage()
function addImage($name, $src)
{
$this->fields[] = array("image", $name, $src);
}
// }}}
// {{{ addHidden()
function addHidden($name, $value)
{
$this->fields[] = array("hidden", $name, $value);
}
// }}}
// {{{ adding misssing addBlank function
function addBlank($i,$title = '')
{
$this->fields[] = array("blank", $i, $title);
}
// {{{ addFile
function addFile($name, $title, $maxsize = HTML_FORM_MAX_FILE_SIZE,
$size = HTML_FORM_TEXT_SIZE, $accept = '')
{
$this->fields[] = array("file", $name, $title, $maxsize, $size, $accept);
}
// {{{ start()
function start()
{
print "<FORM ACTION=\"" . basename($this->action) . "\" METHOD=\"$this->method\"";
if ($this->name) {
print " NAME=\"$this->name\"";
}
if ($this->target) {
print " TARGET=\"$this->target\"";
}
if ($this->enctype) {
print " ENCTYPE=\"$this->enctype\"";
}
print ">\n";
}
// }}}
// {{{ end()
function end()
{
$fields = array();
reset($this->fields);
while (list($i, $data) = each($this->fields)) {
if ($data[0] == 'reset') {
continue;
}
$fields[$data[1]] = true;
}
$this->displayHidden("_fields", implode(":", array_keys($fields)));
print "</FORM>";
}
// }}}
// {{{ displayText()
function displayText($name, $default = '',
$size = HTML_FORM_TEXT_SIZE, $maxlength = '')
{
if (!$maxlength) {
print "<INPUT NAME=\"$name\" VALUE=\"$default\" SIZE=\"$size\"";
} else {
print "<INPUT NAME=\"$name\" VALUE=\"$default\" SIZE=\"$size\" MAXLENGTH=\"$maxlength\"";
}
print ">";
}
// }}}
// {{{ displayTextRow()
function displayTextRow($name, $title, $default = '',
$size = HTML_FORM_TEXT_SIZE, $maxlength = '')
{
print " <TR>\n";
print " <TH ALIGN=\"right\">$title</TH>";
print " <TD>";
$this->displayText($name, $default, $size, $maxlength);
print "</TD>\n";
print " </TR>\n";
}
// }}}
// {{{ displayPassword()
function displayPassword($name, $default = '', $size = HTML_FORM_PASSWD_SIZE)
{
print "<input name=\"$name\" type=\"password\" value=\"$default\" size=\"$size\" />";
}
// }}}
// {{{ displayPasswordRow()
function displayPasswordRow($name, $title, $default = '', $size = HTML_FORM_PASSWD_SIZE)
{
print "<tr>\n";
print " <th align=\"right\">$title:</th>\n";
print " <td>";
$this->displayPassword($name, $default, $size);
print " repeat: ";
$this->displayPassword($name."2", null, $size);
print "</td>\n";
print "</tr>\n";
}
// }}}
// {{{ displayCheckbox()
function displayCheckbox($name, $default = false)
{
print "<input type=\"checkbox\" name=\"$name\"";
if ($default && $default != 'off') {
print " CHECKED";
}
print " />";
}
// }}}
// {{{ displayCheckboxRow()
function displayCheckboxRow($name, $title, $default = false)
{
print " <tr>\n";
print " <th align=\"right\">$title</th>";
print " <td>";
$this->displayCheckbox($name, $default);
print "</td>\n";
print " </tr>\n";
}
// }}}
// {{{ displayTextarea()
function displayTextarea($name, $default = '', $width = 40,
$height = 5, $maxlength = '')
{
if (!$maxlength) {
print "<TEXTAREA NAME=\"$name\" COLS=\"$width\" ROWS=\"$height\"";
} else {
print "<TEXTAREA NAME=\"$name\" COLS=\"$width\" ROWS=\"$height\" MAXLENGTH=\"$maxlength\"";
}
print ">";
print $default;
print "</TEXTAREA>";
}
// }}}
// {{{ displayTextareaRow()
function displayTextareaRow($name, $title, $default = '', $width = 40,
$height = 5, $maxlength = '')
{
print " <TR>\n";
print " <TH ALIGN=\"right\" VALIGN=\"top\">$title</TH>\n";
print " <TD>";
$this->displayTextarea($name, &$default, $width, $height, $maxlength);
print "</TD>\n";
print " </TR>\n";
}
// }}}
// {{{ displaySubmit()
function displaySubmit($title = 'Submit Changes', $name = "submit")
{
print $this->returnSubmit($title, $name);
}
// }}}
// {{{ displaySubmitRow()
function displaySubmitRow($name = "submit", $title = 'Submit Changes')
{
print $this->returnSubmitRow($name, $title);
}
// }}}
// {{{ displayReset()
function displayReset($title = 'Clear contents')
{
print $this->returnReset($title);
}
// }}}
// {{{ displayResetRow()
function displayResetRow($title = 'Clear contents')
{
print $this->returnResetRow($title);
}
// }}}
// {{{ displaySelect()
function displaySelect($name, $entries, $default = '', $size = 1,
$blank = '', $multiple = false, $attribs = '')
{
print $this->returnSelect($name, $entries, $default, $size, $blank,
$multiple, $attribs);
}
// }}}
// {{{ displaySelectRow()
function displaySelectRow($name, $title, &$entries, $default = '',
$size = 1, $blank = '', $multiple = false, $attribs = '')
{
print $this->returnSelectRow($name, $title, $entries, $default, $size,
$blank, $multiple, $attribs);
}
// }}}
// {{{ displayHidden()
function displayHidden($name, $value)
{
print $this->returnHidden($name, $value);
}
// }}}
// assuming that $default is the 'checked' attribut of the radio tag
// {{{ displayRadio()
function displayRadio($name, $value, $default = false)
{
if ($default == false) {
print "<INPUT type='radio' name=\"$name\" value=\"$value\">";
} else {
print "<INPUT type='radio' name=\"$name\" checked value=\"$value\">";
}
}
// }}}displayRadio()
// {{{ displayRadioRow()
function displayRadioRow($name, $title, $value, $default = false)
{
print " <TR>\n";
print "<TH ALIGN=\"right\">$title</TH>";
print " <TD>";
$this->displayRadio($name, $value, $default);
print "</TD>\n";
print " </TR>\n";
}
// }}}displayRadioRow()
// {{{ displayBlank()
function displayBlank()
{
print " ";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -