OOP Form
Ik kwam onderstaand script op internet tegen, ik ben net begonnen met OOP, en ik was benieuwd wat jullie mening erover is en of er betere alternatieven zouden zijn of dat dit juist is.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**********************************/
/***** Form Create using OOP *******/
/**********************************/
/**********************************/
class form {
//Input Attributes
public $type;
public $name;
public $value;
public $text;
public $opttext;
//Select Methods
public $optValues = array();
public $starttag;
public $action;
public $method;
/* form tag */
function formtag() {
if($this->starttag=='true') {
echo "<form action='". $this->action ."' method='". $this->method ."'>";
} else {
echo "</form>";
}
}
//Input Fields
function input() {
if($this->type=='text') {
echo $this->text."<input type='". $this->type ."' name='". $this->name ."' value='". $this->value ."'> \n";
}
elseif ( $this->type=='select') {
echo $this->text."<select name='". $this->name ."'> \n";
for($i=0; $i< count($this->optValues);$i++ ) {
echo "<option value='". $this->optValues[$i] ."'>". $this->optValues[$i] ." </option> \n";
}
echo "</select> \n";
}
elseif($this->type=='radio' || $this->type=='checkbox') {
echo $this->text."<input type='". $this->type ."' name='". $this->name ."' value='". $this->value ."'>".$this->opttext." \n";
}
elseif($this->type=='button' || $this->type=='submit') {
echo "<input type='". $this->type ."' name='". $this->name ."' value='". $this->value ."'> \n";
}
else {
echo "Please enter the type";
}
}//method end.
function br() {
echo "<br />";
}//if end
}//formFields class end.
$formInputs = new form;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>OOPs Form</title>
</head>
<body>
<?php
$formInputs->starttag='true';
$formInputs->action ='';
$formInputs->method ='post';
$formInputs->formtag();
$formInputs->text = "Name";
$formInputs->type = "text";
$formInputs->name = "";
$formInputs->value = "";
$formInputs->input();
$formInputs->br();
$formInputs->text = "DOB";
$formInputs->type = "text";
$formInputs->name = "";
$formInputs->value = "";
$formInputs->input();
$formInputs->br();
$formInputs->text = "Gender";
$formInputs->type = "radio";
$formInputs->name = "gender";
$formInputs->opttext = "Male";
$formInputs->input();
$formInputs->text = "";
$formInputs->type = "radio";
$formInputs->name = "gender";
$formInputs->opttext = "Female";
$formInputs->input();
$formInputs->br();
$formInputs->text = "Country";
$formInputs->type = "select";
$formInputs->name = "country";
$formInputs->optValues[] = 'Pakistan'; $formInputs->optValues[] = 'Afganistan'; $formInputs->optValues[] = 'usa';
$formInputs->optValues[] = 'Srilanka'; $formInputs->optValues[] = 'Malashia'; $formInputs->optValues[] = 'Rrussia';
$formInputs->input();
$formInputs->br();
$formInputs->type = "submit";
$formInputs->name = "";
$formInputs->value = "Submit";
$formInputs->input();
$formInputs->br();
$formInputs->starttag='false';
$formInputs->formtag();
?>
</body>
</html>
/**********************************/
/***** Form Create using OOP *******/
/**********************************/
/**********************************/
class form {
//Input Attributes
public $type;
public $name;
public $value;
public $text;
public $opttext;
//Select Methods
public $optValues = array();
public $starttag;
public $action;
public $method;
/* form tag */
function formtag() {
if($this->starttag=='true') {
echo "<form action='". $this->action ."' method='". $this->method ."'>";
} else {
echo "</form>";
}
}
//Input Fields
function input() {
if($this->type=='text') {
echo $this->text."<input type='". $this->type ."' name='". $this->name ."' value='". $this->value ."'> \n";
}
elseif ( $this->type=='select') {
echo $this->text."<select name='". $this->name ."'> \n";
for($i=0; $i< count($this->optValues);$i++ ) {
echo "<option value='". $this->optValues[$i] ."'>". $this->optValues[$i] ." </option> \n";
}
echo "</select> \n";
}
elseif($this->type=='radio' || $this->type=='checkbox') {
echo $this->text."<input type='". $this->type ."' name='". $this->name ."' value='". $this->value ."'>".$this->opttext." \n";
}
elseif($this->type=='button' || $this->type=='submit') {
echo "<input type='". $this->type ."' name='". $this->name ."' value='". $this->value ."'> \n";
}
else {
echo "Please enter the type";
}
}//method end.
function br() {
echo "<br />";
}//if end
}//formFields class end.
$formInputs = new form;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>OOPs Form</title>
</head>
<body>
<?php
$formInputs->starttag='true';
$formInputs->action ='';
$formInputs->method ='post';
$formInputs->formtag();
$formInputs->text = "Name";
$formInputs->type = "text";
$formInputs->name = "";
$formInputs->value = "";
$formInputs->input();
$formInputs->br();
$formInputs->text = "DOB";
$formInputs->type = "text";
$formInputs->name = "";
$formInputs->value = "";
$formInputs->input();
$formInputs->br();
$formInputs->text = "Gender";
$formInputs->type = "radio";
$formInputs->name = "gender";
$formInputs->opttext = "Male";
$formInputs->input();
$formInputs->text = "";
$formInputs->type = "radio";
$formInputs->name = "gender";
$formInputs->opttext = "Female";
$formInputs->input();
$formInputs->br();
$formInputs->text = "Country";
$formInputs->type = "select";
$formInputs->name = "country";
$formInputs->optValues[] = 'Pakistan'; $formInputs->optValues[] = 'Afganistan'; $formInputs->optValues[] = 'usa';
$formInputs->optValues[] = 'Srilanka'; $formInputs->optValues[] = 'Malashia'; $formInputs->optValues[] = 'Rrussia';
$formInputs->input();
$formInputs->br();
$formInputs->type = "submit";
$formInputs->name = "";
$formInputs->value = "Submit";
$formInputs->input();
$formInputs->br();
$formInputs->starttag='false';
$formInputs->formtag();
?>
</body>
</html>
heeft niets met OOP te maken, het is gewoon een CLASS die wat smerige html genereert.. overigens mis je een hoop functionaliteiten (selected items etc)
Een heel goed alternatief is de Symfony formbuilder, scheelt een hoop programmeer werk ;)
Code (php)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
<?php
$form = new Form();
$textbox = new Form_Element_Textbox(); //Form_Element_Textbox extends de class Form_Element
$form->addElement($textbox);
echo $form->render(); //Render alle fields
?>
$form = new Form();
$textbox = new Form_Element_Textbox(); //Form_Element_Textbox extends de class Form_Element
$form->addElement($textbox);
echo $form->render(); //Render alle fields
?>
Thanks, vraag hoe ziet dan je Class eruit?
Bas D L op 01/11/2012 13:02:42:
@Raoul,
Thanks, vraag hoe ziet dan je Class eruit?
Thanks, vraag hoe ziet dan je Class eruit?
Iedere form element heeft een render method, omdat natuurlijk iedere method een andere HTML geeft. In de render method dan, in de form class, dan stop ik iedere render van iedere element in een variable en return ik die.