Evo kako sam ja napravio FORM klasu. Formular se organizuje u tabelu kojoj se izgled moze menjati uz pomoc CSS-a. Svaki element tabele i formulara ima class i id atribut.
detalji formilara (class, id, action, post/get) se podesavaju u konstruktoru obavezno;
SetTable() sluzi za podesavanje tabele u kojoj ce biti elementi formulara
wrap() metoda "umotava" elemente formulara koje ste dodali u jednu celiju tabele(desnu celiju) i daje opis (na screenshotu to je leva kolona tabele). Svaki put kad se ova metoda pozove, umotaju se elementi koji nisu "umotani" ranije
insert_code() sluzi za ubacibanje nekog HTML koda izmedju elemenata, recimo <br> za nov red
display() iscrtava formular
ostale metode dodaju elemente forme. posebno je zaniumljiva ova: adddropdownbox() koja dodaje padajuci meni i sve opcije u jedno koraku. Opcije se salju u asocijativnom nizu.
Ovo sam odradio za jedan dan, pa je verovatno puno kojekakvih propusta.
Molim za komentare!
Code:
<?php
class Form{
var $action;
var $f_class;
var $id;
var $method;
var $table_id;
var $table_class;
var $title_column_class;
var $content_column_class;
var $content;
var $not_wrapped;
function Form($action, $class_par, $id, $method = "POST"){
if($method!="POST" and $method!="GET"){
die("Form method error!");
}
$this->action = $action;
$this->f_class = $class_par;
$this->id = $id;
$this->method = $method;
}
function SetTable($table_class_par, $table_id_par, $title_column_class_par, $content_column_class_par){
$this->table_id = $table_class_par;
$this->table_class = $table_id_par;
$this->title_column_class = $title_column_class_par;
$this->content_column_class = $content_column_class_par;
}
function wrap($text=""){
if($this->not_wrapped != ""){
$this->content .= "<tr>\n";
$this->content .= "<td class='$this->title_column_class'>$text</td>\n";
$this->content .= "<td class='$this->content_column_class'>$this->not_wrapped</td>\n";
$this->content .= "</tr>\n";
$this->not_wrapped = "";
}
}
function insert_code($code){
$this->not_wrapped .= $code;
}
function display(){
echo "<form action='$this->action' class='$this->f_class' id=' $this->id' method='$this->method'>"; //form start
echo "<table id='$this->table_id' class='$this->table_class'>"; //table start
if($this->not_wrapped != "") $this->wrap();
echo $this->content;
echo "</table>";
echo "</form>";
}
//elementi forme
function addtextbox($name, $size, $value, $class, $id, $wrap_it="_no_wrapping"){
$this->not_wrapped .= "<input type='text' name='$name' size='$size' value='$value' class='$class' id='$id'>";
if ($wrap_it!="_no_wrapping") $this->wrap($wrap_it);
}
function addtextarea($name, $rows, $cols, $value, $class, $id, $wrap_it="_no_wrapping"){
$this->not_wrapped .= "<textarea rows='$rows' name='$name' cols='$cols' class='$class' id='$id'>$value</textarea>";
if ($wrap_it!="_no_wrapping") $this->wrap($wrap_it);
}
function addcheckbox($name, $value, $class, $id, $text, $wrap_it="_no_wrapping"){
$this->not_wrapped .= "<input type='checkbox' name='$name' class='$class' id='$id' value='$value'>$text";
if ($wrap_it!="_no_wrapping") $this->wrap($wrap_it);
}
function addradiobutton($group_name, $value, $class, $id, $text, $checked, $wrap_it="_no_wrapping"){
if($checked==0){
$this->not_wrapped .= "<input type='radio' value='$value' name='$group_name' class='$class' id='$id'>$text";
}else{
$this->not_wrapped .= "<input type='radio' value='$value' checked name='$group_name' class='$class' id='$id'>$text";
}
if ($wrap_it!="_no_wrapping") $this->wrap($wrap_it);
}
function adddropdownbox($name, $size, $value_array, $sel_val, $class, $id, $multiple, $wrap_it="_no_wrapping"){
$this->not_wrapped .= "<select size='$size' name='$name' class='$class' id='$id'";
if($multiple == 1) $this->not_wrapped .= "multiple";
$this->not_wrapped .= ">\n";
foreach($value_array as $asoc => $val){
if($val!=$sel_val){
$this->not_wrapped .= "<option value='$val'>$asoc</option>\n";
}else{
$this->not_wrapped .= "<option selected value='$val'>$asoc</option>\n";
}
}
$this->not_wrapped .= "</select>\n";
if ($wrap_it!="_no_wrapping") $this->wrap($wrap_it);
}
function addbutton($name, $value, $type, $class, $id, $wrap_it="_no_wrapping"){
switch ($type){
case 0 :
$this->not_wrapped .= "<input type='button' value='$value' name='$name' class='$class' id='$id'>";
break;
case 1 :
$this->not_wrapped .= "<input type='submit' value='$value' name='$name' class='$class' id='$id'>";
break;
case 2 :
$this->not_wrapped .= "<input type='reset' value='$value' name='$name' class='$class' id='$id'>";
break;
default :
$this->not_wrapped .= "<input type='button' value='$value' name='$name' class='$class' id='$id'>";
break;
}
if ($wrap_it!="_no_wrapping") $this->wrap($wrap_it);
}
};
?>
i evo primena:
Code:
<?php
include ('./form.class.php');
$t = new Form($_SERVER['PHP_SELF'], "klasa", "ajdi");
$t->settable("4", "tabele", "title", "content");
$t->addtextbox("name", "45", "value", "class", "id", "Ime : ");
$t->addtextbox("name", "40", "value", "class", "id", "Prezime : ");
$t->addtextbox("name", "40", "value", "class", "id", "Adresa : ");
$t->addtextbox("name", "40", "value", "class", "id");
$t->wrap("neki text jedan: ");
$t->addtextarea("alo", 10, 20, "Neka vrednost\njos neka vrednost", "klasa", "id", "wrap");
$t->addcheckbox("juhu", "ON", "klasica", "ajdi", "tekst pored");
$t->insert_code(" - ");
$t->addcheckbox("juhu", "ON", "klasica", "ajdi", "tekst pored", "opis");
$t->addradiobutton("grupa1", 1, "klasa", "ajdi", "tekstic pored:", 0);
$t->insert_code("<br>\n");
$t->addradiobutton("grupa1", 1, "klasa", "ajdi", "tekstic pored:", 0);
$t->insert_code("<br>");
$t->addradiobutton("grupa1", 1, "klasa", "ajdi", "tekstic pored:", 0, "45454");
$t->addradiobutton("grupa2", 1, "klasa", "ajdi", "tekstic pored:", 0);
$t->insert_code(" - ");
$t->wrap("neki text : ");
$arr['ime']="Djordje";
$arr['neki_kurac']="Vrednost";
$t->adddropdownbox("ime", 1, $arr, "Vrednost", "klasa", "ajdi", 0, "neki tekstic");
$t->addbutton("ime", "Dalje", 1, "klasa", "id", "wrapping:");
$t->display();
?>
a evo kako to izgleda (uz jako los CSS

):
http://static.elitesecurity.org/uploads/1/4/1400397/form.JPG
Evo jos jedan primer upotrebe:
Code:
<?php
include ('./form.class.php');
$form = new Form($_SERVER['PHP_THIS'], "klasa", "id");
$form->settable("4", "tabele", "title", "content");
$form->addtextbox("username", "25", "", "", "", "Korisnicko ime : ");
$form->addtextbox("password", "25", "", "", "", "Sifra : ");
$form->addbutton("login_button", "Login", 1, "", "", "");
$form->display();
?>
i to izgleda ovako:
http://static.elitesecurity.org/uploads/1/4/1400397/form2.JPG
[Ovu poruku je menjao djordje dana 20.12.2006. u 03:41 GMT+1]