class koeform {
public $form = '';
public $act;
public $method;
public $formname;
public $hiden = array();
public $formid;
public $legend;
function __construct($act, $method = '', $formname = '', $formid = '', $hidden = '', $legend = '') {
$this->act = $act;
$this->method = $method ? $method : 'post';
$this->formname = $formname ? $formname : '';
$this->formid = $formid ? $formid : '';
$this->hidden = $hidden ? $hidden : '';
$this->legend = $legend ? $legend : '';
}
function init() {
$form = '<form action="' . $this->act . '" method="' . $this->method . '" ';
$form .= $this->formid ? 'id="' . $this->formid . '" ' : '';
$form .= $this->formname ? 'name="' . $this->formname . '">' : '>';
$form .= $this->legend ? '<fieldset><legend>' . $this->legend . '</legend>' : '';
if (sizeof($this->hidden)) {
foreach ($this->hidden as $k => $v) {
$form .= PHP_EOL . '<input type="hidden" name="' . $k . '" value="' . $v . '" />' . PHP_EOL;
}
}
return $this->form = $form . PHP_EOL;
}
function inputform($name, $text = '', $value = '', $size = 20, $max = '', $readonly = 0) {
$form = '';
$form .= $text ? '<b>' . $text . '</b><br/>' : '';
$form .= '<input type="text" name="' . $name . '" value="' . $value . '" size="' . $size . '" ';
$form .= $readonly ? 'readonly="readonly" ' : '';
$form .= $max ? 'maxlength="' . $max . '" /><br/>' : '/><br/>';
return $this->form .= PHP_EOL . $form . PHP_EOL;
}
function textareaform($name, $text = '', $value = '', $rows = 5, $cols = 40, $readonly = 0) {
$form = '';
$form .= $text ? '<b>' . $text . '</b><br/>' : '';
$form .= '<textarea name="' . $name . '" rows="' . $rows . '" cols="' . $cols . '" ' . ($readonly ? 'readonly="readonly"' : '') . '>';
$form .= $value ? $value : '';
$form .= '</textarea><br/>';
return $this->form .= PHP_EOL . $form . PHP_EOL;
}
function selectform ($name, $param, $selected = 1) {
if (!is_array($param)) {
$form = '';
} else {
$form = '<select name="' . $name . '">' . PHP_EOL;
$x = 1;
foreach ($param as $k => $v) {
$form .= '<option value="' . $v . '" ' . ($x==$selected ? 'selected="selected"' : '') . '>' . $k . '</option>' . PHP_EOL;
$x++;
}
$form .= '</select><br/>';
}
return $this->form .= $form . PHP_EOL;
}
function formprint($submitname = 'submit', $submitvalue = 'submit') {
$form = '<input type="submit" name="' . $submitname . '" value="' . $submitvalue . '" />' . ($this->legend ? '</fieldset>' : '') . '</form>';
return $this->form . PHP_EOL . $form . PHP_EOL;
}
}
Ну и сама форма.
$form = new koeform($_SERVER['PHP_SELF'], '', 'testform', 'formid', array('username' => 'vasya', 'userid' => 555), 'Класс форм');
$form->init();
$form->inputform('test', 'Testing form', 'text');
$form->inputform('test2', 'Testing form2', '', 30, 25);
$form->inputform('test2', 'Testing form readonly', 'readonly test', 30, 25, 1);
$form->textareaform('test3', 'Testing form3', 'text3');
$form->textareaform('test4', 'Testing form4', 'readonly textarea' . PHP_EOL . 'произвольный текст', 7, 60, 1);
$form->selectform('select', array('картошка' => 25, 'капуста' => 15, 'морковка' => 10), 2);
$a = $form->formprint();
echo nl2br(htmlspecialchars($a));
echo '<br/>' . $a;
Сейчас подробно постараюсь рассказать.