|
<? Php
// Form. php
Class form {
Var $ layout = true; // whether to use table layout
Var $ action; // URL to which the form is submitted
Var $ method;
Var $ enctype = "";
Var $ name = "";
Var $ id = "";
Var $ class = "";
Function form ($ action, $ method = "POST") {// initialize the member variable through the constructor
$ This-> action = $ action;
$ This-> method = $ method;
}
Function form_start (){
$ Text = "<form action = \" {$ this-> action} \ "method = \" {$ this-> method }\"";
If ($ this-> class! = ""){
$ Text. = "class = \" {$ this-> class }\"";
}
If ($ this-> enctype! = ""){
$ Text. = "enctype = \" {$ this-> enctype }\"";
}
If ($ this-> id! = ""){
$ Text. = "id = \" {$ this-> id }\"";
}
If ($ this-> name! = ""){
$ Text. = "name = \" {$ this-> name }\"";
}
$ Text. = "> \ n ";
If ($ this-> layout = true ){
$ Text. = "<table> \ n ";
}
Return $ text;
}
Function form_end (){
If ($ this-> layout = true ){
$ Text = "\ t </table> \ n ";
$ Text. = "</form> \ n ";
} Else {
$ Text = "</form> \ n ";
}
Return $ text;
}
// Text box function
Function form_text ($ name, $ id, $ label_name, $ label_for, $ value = ""){
$ Text = "<input type = \" text \ "name = \" {$ name }\"";
$ Text. = "id = \" {$ id }\"";
If (isset ($ value )){
$ Text. = "value = \" {$ value }\"";
}
$ Text. = "/> \ n ";
$ Label = $ this-> form_label ($ label_name, $ label_for );
$ Form_item = $ this-> form_item ($ label, $ text );
Return $ form_item;
}
// Password box function
Function form_passwd ($ name, $ id, $ label_name, $ label_for, $ value = ""){
$ Text = "<input type = \" password \ "name = \" {$ name }\"";
$ Text. = "id = \" {$ id }\"";
If (isset ($ value )){
$ Text. = "value = \" {$ value }\"";
}
$ Text. = "/> \ n ";
$ Label = $ this-> form_label ($ label_name, $ label_for );
$ Form_item = $ this-> form_item ($ label, $ text );
Return $ form_item;
}
// Hide the Domain Function
Function form_hidden ($ name, $ id, $ label_name, $ label_for, $ value = ""){
$ Text = "<input type = \" hidden \ "name = \" {$ name} \ "id = \" {$ id }\"";
If (isset ($ value )){
$ Text. = "value = \" {$ value }\"";
}
$ Text. = "/> \ n ";
$ Label = $ this-> form_label ($ label_name, $ label_for );
$ Form_item = $ this-> form_item ($ label, $ text );
Return $ form_item;
}
// File domain Functions
Function form_file ($ name, $ id, $ label_name, $ label_for, $ size = ""){
$ Text = "<input type = \" file \ "name = \" {$ name }\"";
$ Text. = "id = \" {$ id }\"";
If (isset ($ size )){
$ Text. = "size = \" {$ size }\"";
}
$ Text. = "/> \ n ";
$ Label = $ this-> form_label ($ label_name, $ label_for );
$ Form_item = $ this-> form_item ($ label, $ text );
Return $ form_item;
}
// Check box function
Function form_checkbox ($ name, $ label = array (), $ label_name, $ label_for = ""){
$ I = 0;
$ Text = array ();
Foreach ($ label as $ id => $ value ){
$ Text [$ I] = "<input type = \" checkbox \ "id = \" {$ id} \ "name = \" {$ name} \ "value = \ "{$ value} \"/> ";
$ Text [$ I]. = "<label for = \" {$ id} \ ">{$ value} </label> ";
$ I ++;
}
$ Label = $ this-> form_label ($ label_name, $ label_for );
$ Form_item = $ this-> form_item ($ label, $ text );
Return $ form_item;
}
// Single Functions
Function form_radio ($ name, $ label = array (), $ label_name, $ label_for = ""){
$ I = 0;
$ Text = array ();
Foreach ($ label as $ id => $ value ){
$ Text [$ I] = "<input type = \" radio \ "id = \" {$ id} \ "name = \" {$ name} \ "value = \ "{$ value} \"/> ";
$ Text [$ I]. = "<label for = \" {$ id} \ ">{$ value} </label> ";
$ I ++;
}
$ Label = $ this-> form_label ($ label_name, $ label_for );
$ Form_item = $ this-> form_item ($ label, $ text );
Return $ form_item;
}
// Drop-down menu Function
Function form_select ($ id, $ name, $ options = array (), $ selected = false, $ label_name, $ label_for, $ onchange = ""){
If ($ onchange! = ""){
$ Text = "<select id = \" {$ id} \ "name = \" {$ name} \ "onchang = \" {$ onchange} \ "> \ n";
}
Else {
$ Text = "<select id = \" {$ id} \ "name = \" {$ name} \ "> \ n ";
}
Foreach ($ options as $ value => $ key ){
If ($ selected = $ value ){
$ Text. = "\ t <option valute = \" {$ value} \ "selected = \" selected \ ">{$ key} </option> \ n ";
} Elseif ($ selected === false ){
$ Text. = "\ t <option value = \" {$ value} \ ">{$ key} </option> \ n ";
}
}
$ Text. = "</select> ";
$ Label = $ this-> form_label ($ label_name, $ label_for );
$ Form_item = $ this-> form_item ($ label, $ text );
Return $ form_item;
}
// Multiple-choice list Function
Function form_selectmul ($ id, $ name, $ size, $ options = array (), $ label_name, $ label_for ){
$ Text = "<select id = \" {$ id} \ "name = \" {$ name} \ "size = \" {$ size} \ "multiple = \" multiple \ "> \ n ";
Foreach ($ options as $ value => $ key ){
$ Text. = "\ t <option value = \" {$ value} \ ">{$ key} </option> \ n ";
}
$ Text. = "</select> \ n ";
$ Label = $ this-> form_label ($ label_name, $ label_for );
$ Form_item = $ this-> form_item ($ label, $ text );
Return $ form_item;
}
// Button Function
Function form_button ($ id, $ name, $ type, $ value, $ onclick = ""){
$ Text = "<button id = \" {$ id} \ "name = \" {$ name} \ "type = \" {$ type }\"";
If ($ onclick! = ""){
$ Text. = "onclick = '{$ onclick }'";
}
$ Text. = ">". $ value;
$ Text. = "</button> \ n ";
If ($ this-> layout = true ){
$ Form_item = "<tr> \ n \ t <th> </th> <td >{$ text} </td> \ n </tr> \ n ";
} Else {
$ Form_item = $ text;
}
Return $ form_item;
}
// Text domain Functions
Function form_textarea ($ id, $ name, $ cols, $ rows, $ label_name, $ label_for, $ value = ""){
$ Text = "<textarea id = \" {$ id} \ "name = \" {$ name} \ "cols = \" {$ cols} \ "rows = \" {$ rows} \ ">{$ value} </textarea> \ n ";
$ Label = $ this-> form_label ($ label_name, $ label_for );
$ Form_item = $ this-> form_item ($ label, $ text );
Return $ form_item;
}
// Text tag Function
Function form_label ($ text, $ ){
If ($! = ""){
$ Label = "<label for = \" {$ for} \ ">{$ text }:</label> ";
} Else {
$ Label = $ text .":";
}
Return $ label;
}
Function form_item ($ form_label, $ form_text ){
Switch ($ this-> layout ){
Case true:
$ Text = "<tr> \ n ";
$ Text. = "\ t <th class = \" label \ "> ";
$ Text. = $ form_label;
$ Text. = "</th> \ n ";
$ Text. = "\ t <td> ";
$ Text. = $ form_text;
$ Text. = "</td> \ n ";
$ Text. = "</tr> \ n ";
Break;
Case false:
$ Text = $ form_label;
$ Text. = $ form_text;
Break;
}
Return $ text;
}
Function CreateForm ($ form_item = array ()){
Echo $ this-> form_start ();
Foreach ($ form_item as $ item ){
Echo $ item;
}
Echo $ this-> form_end ();
}
}
?>
|