Sådan benyttes komponenten Form klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Form.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Form::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Form($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Form klassen
Den fulde PHP kildekode for Form klassen
<?php/** * @package form * @filesource * @see HTML_FORM_COMPONENT_PATH.'/Form.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * The required files */require_once(HTML_BASE_COMMON_PATH.'/Html.php');/** * Generates a html form * <code> * Usage: * $action = Form::METHOD_GET; * $method = $this->method; * $name = $this->getClassName(); * $attr = ""; * $title = ""; * $onsubmit = "alert(this.name); return true;"; * $enctype = ""; * $target = ""; * * $form = new Form($action, $method, $name, $attr, $title, $onsubmit, $enctype, $target); * $form->add(new SubmitButton($name, $value, $class, $disabled, $onclick, $title, $tabindex, $accesskey)); * print $form->getHtml(); * Or: * Form::start($action, $method, $name, $attr, $title, $onsubmit, $enctype, $target); * elements::display() * Form::end(); * </code> * @package form */class Form extends Html { const METHOD_GET = "get"; const METHOD_POST = "post"; /** * @var String $action The action for the post or get request, where to go */ protected $action = ''; /** * @var String $method How to send data POST/GET */ protected $method = ''; /** * @var String $name The name of the form (NOT xhtml 1.0 strict compliant) */ protected $name = ''; /** * @var String $id The id of the form (NOT netscape 4.x compliant) */ protected $id = ''; /** * @var String $attr Additional attributes for the form */ protected $attr = ''; /** * @var String $title The title (tooltip) of the form */ protected $title = ''; /** * @var String $onsubmit The Onsubmit event */ protected $onsubmit = ''; /** * @var String $enctype The encoding type: multipart/form-data */ protected $enctype = ''; /** * @var String $target The name of the target window */ protected $target = ''; /** * Constructor * @param String $action The action where to go, default 'self' * @param String $method The method get/post * @param String $name The name if any (NOT xhtml 1.0 strict compliant) * @param String $attr Additional attributes, i.e. 'enctype="multipart/form-data"' * @param String $title The title (tooltip) * @param String $onsubmit The onsubmit event * @param String $enctype The encoding type * @param String $target The name of the target window */ function __construct($action='', $method='', $name='', $attr='', $title='', $onsubmit='', $enctype='', $target='') { parent::__construct(); $this->method = $method != '' ? $method : self::METHOD_GET; $this->name = $name != '' ? $name : ''; $this->attr = $attr != '' ? ' '.$attr : ''; $this->title = $title != '' ? $title : ''; $this->onsubmit = $onsubmit != '' ? $onsubmit : ''; $this->action = $this->getAction($action); $this->enctype = $enctype != '' ? $enctype : ''; $this->target = $target != '' ? $target : ''; if ($this->enctype === 'multipart/form-data' && $this->method !== Form::METHOD_POST) { die($this->getClassName()."() must use POST when using enctype=='multipart/form-data'"); } if (defined('SHOW_ELEMENT_ID') && SHOW_ELEMENT_ID) { $this->id = Element::id($this->getClassName()); if ($this->name == '') { $this->name = $this->id; // NOT xhtml 1.0 strict compliant } } } /** * Get and modify the action value, special case for form post * Strip off any request parameters * Add a terminating slash if not already there * Add index.php if not already there * Samples: * '' index.php prices prices/ /prices /prices/ prices/index.php * http://hvepse.dk http://hvepse.dk/ http://hvepse.dk/index.php * http://hvepse.dk/price http://hvepse.dk/price/ http://hvepse.dk/price/index.php * Note: The DIBS cashreg.pfml is ignored * * @param String $action The action from the constructor * @return String The modified action for post */ function getAction($action) { $theAction = $action != '' ? $action : $_SERVER['PHP_SELF']; if ($this->method == Form::METHOD_POST) { $url = explode('?', $theAction); $theAction = $url[0]; if ($theAction == '') { $theAction .= INDEX_FILE_NAME; } if (strpos($theAction, INDEX_FILE_NAME) === false && strpos($theAction, CASHREG_FILE_NAME) === false) { if (strpos($theAction, '/') === false) { $theAction .= '/'; // Add ending slash } if (strrpos($theAction, '/') !== strlen($theAction) -1) { $theAction .= '/'; // Add ending slash } $theAction .= INDEX_FILE_NAME; } } return $theAction; } /** * Returns the html for the start of a form * @return String The html */ function getStart() { $html = ''; $html .= '<form'; $html .= $this->getAttribute('action'); $html .= $this->getAttribute('method'); $html .= $this->getAttribute('name'); // NOT xhtml 1.0 strict compliant $html .= $this->getAttribute('id'); // NOT netscape 4.x compliant $html .= $this->getAttribute('title'); $html .= $this->getAttribute('onsubmit'); $html .= $this->getAttribute('enctype'); $html .= $this->getAttribute('target'); $html .= $this->attr; // more attributes, if required $html .= '>'."\r\n"; $html .= $this->getElements(); // as html return $html; } /** * Returns the html for the end of a form * @return String The html */ function getEnd() { return "</form>\r\n"; } /** * Get the complete html for a Form * @return String the html */ function getHtml() { $html = $this->html; $html .= $this->getStart(); $html .= $this->getEnd(); return $html; } /** * Display start html * <code> * Usage: * Form::start($action, $method, $name, $attr, $title, $onsubmit, $enctype); * </code> * @static * @param String $action The action where to go, default 'self' * @param String $method The method get/post * @param String $name The name if any (NOT xhtml 1.0 strict compliant) * @param String $attr Additional attributes, i.e. 'enctype="multipart/form-data"' * @param String $title The title (tooltip) * @param String $onsubmit The onsubmit event * @param String $enctype The encoding type */ public static function start($action='', $method='', $name='', $attr='', $title='', $onsubmit='', $enctype='') { switch ($action) { case 'fieldset': if (defined('HTML_BASE_UTIL_PATH')) { Fieldset::start($action, $method); } break; default: $html = new Form($action, $method, $name, $attr, $title, $onsubmit, $enctype); $html->addHtml($html->getStart()); break; } } /** * Display end html * <code> * Usage: * Form::end($element); * </code> * @static */ public static function end($element='') { switch ($element) { case 'fieldset': if (defined('HTML_BASE_UTIL_PATH')) { Fieldset::end(); } break; default: $html = new Form(); $html->addHtml($html->getEnd()); break; } } /** * Display html * <code> * Usage: * Form::display($action, $method, $name, $attr, $title, $onsubmit, $enctype, $target); * </code> * @static * @param String $action The action where to go, default 'self' * @param String $method The method get/post * @param String $name The name if any (NOT xhtml 1.0 strict compliant) * @param String $attr Additional attributes, i.e. 'enctype="multipart/form-data"' * @param String $title The title (tooltip) * @param String $onsubmit The onsubmit event * @param String $enctype The encoding type * @param String $target The name of the target window */ public static function display($action='', $method='', $name='', $attr='', $title='', $onsubmit='', $enctype='', $target='') { $html = new Form($action, $method, $name, $attr, $title, $onsubmit, $enctype, $target); $html->addHtml($html->getHtml()); }}?>
Den fulde HTML kildekode for Form klassen
<? <!-- DEBUG: Form --> <form action="/source-code/form/Form/index.php" method="get" name="Form1" id="Form1"> </form> ?>
Her er 'klasse metoderne' for Form klassen:
Her er 'objekt variable' for Form klassen: