blank.gif
triangle.gif Du er her: /  Forsiden  /  Kildekoden  /  Form  /  Form   Login nu   Login
blank.gif
 ««« Se kilde koden
blank.gif
triangle.gif Basic Base Component Db Dto Form  Form-elements Jquery Layout Menu Menu-fisheye Mvc Tab Table Template Util
blank.gif
blank.gif
 
arrow-headline.gif Index
 
  Tilbage

Navn : Form.php


Sample code, tutorial

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();
    ?>

Parent html

Sådan vises komponenten Form klassen


PHP source code

Den fulde PHP kildekode for Form klassen

<?
/**
* @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.10
* @since 22-feb-2007
*/

/**
* The required files
*/
require_once(HTML_BASE_COMMON_PATH.'/Html.php');

/**
* Generates a html form
* <code>
* Usage:
*   $form = new Form('goHere.php','get');
*   $form->add(new SubmitButton($action,$method,$name,$attr,$title,$onsubmit,$enctype));
*   print $form->getHtml();
* Or:
*   Form::start($action,$method,$name,$attr,$title,$onsubmit,$enctype);
*   elements::display()
*   Form::end();
* </code>
* @package form
*/

class Form extends Html {
    var
$action   = ''; // Where to go
    
var $method   = ''; // How to send data POST/GET
    
var $name     = ''; // The name of the form (NOT xhtml 1.0 strict compliant)
    
var $id       = ''; // The id of the form (NOT netscape 4.x compliant)
    
var $attr     = ''; // Additional attributes for the form
    
var $title    = ''; // The title (tooltip) of the form
    
var $onsubmit = ''; // Onsubmit event
    
var $enctype  = ''; // The encoding type: multipart/form-data

    /**
     * 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
     */
    
function Form($action='',$method='',$name='',$attr='',$title='',$onsubmit='',$enctype='') {
        
$this->Html();
        
$this->method   = $method!=''?$method:FORM_METHOD_GET;
        
$this->action   = $this->getAction($action);
        
$this->name     = $name!=''?$name:'';
        
$this->attr     = $attr!=''?' '.$attr:'';
        
$this->title    = $title!=''?$title:'';
        
$this->onsubmit = $onsubmit!=''?$onsubmit:'';
        
$this->enctype  = $enctype;
        if (
$this->enctype=='multipart/form-data' && $this->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
     * @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) {
                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->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
     */
    
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
     */
    
function end($element='') {
        switch (
$element) {
            case
'fieldset':
                if (
defined('HTML_BASE_UTIL_PATH')) {
                    
Fieldset::end();
                }
                break;
            default:
                
$html = new Html();
                
$html->addHtml(Form::getEnd());
                break;
        }
    }

    
/**
     * Display html
     * <code>
     * Usage:
     *    Form::display($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
     */
    
function display($action='',$method='',$name='',$attr='',$title='',$onsubmit='',$enctype='') {
        
$html = new Form($action,$method,$name,$attr,$title,$onsubmit,$enctype);
        
$html->addHtml($html->getHtml());
    }

}
?>

HTML source code

Den fulde HTML kildekode for Form klassen

<?
<form action="/source-code/form/Form/index.php" method="get" name="Form51" id="Form51">
</
form>

?>

Class methods

Her er 'klasse metoderne' for Form klassen:

  • object
  • getclassname
  • getmsg
  • addhtml
  • gethtml
  • tostring
  • getcachefilename
  • save
  • content
  • html
  • setobject
  • set
  • get
  • getattribute
  • gettag
  • add
  • getsizeof
  • getelement
  • getelements
  • gettoogle
  • getmaximize
  • getminimize
  • newtriangle
  • display
  • showsource
  • form
  • getaction
  • getstart
  • getend
  • start
  • end

Object vars

Her er 'objekt variable' for Form klassen:

  • html =>
  • sql =>
  • elements => Array
  • sizeof => 0
  • action => /source-code/form/Form/index.php
  • method => get
  • name => Form51
  • id => Form51
  • attr =>
  • title =>
  • onsubmit =>
  • enctype =>

 
triangle.gif

danmark

Germany

England

France

Italy

Norge

Sverige

USA


 
blank.gif