PHP automatically generated form code sharing

Source: Internet
Author: User

PHP automatically generated form code sharing

During PHP development, we sometimes encounter forms that are not fixed and need to be dynamically generated during the running of the program. Here we will share a class for PHP to automatically create forms for you.

Form. 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

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

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

}

}

?>

Example:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<! 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" xml: lang = "zh" lang = "zh">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>

<Link rel = "stylesheet" type = "text/css" href = "css/styles.css"/>

<Title> User Logon </title>

</Head>

<Body>

<? Php

Require_once ("form. php ");

$ Form = new form ($ _ SERVER ['php _ SELF ']); // submit to this page

$ Form-> layout = false; // No table layout is used. You can comment out this sentence to see if the result is different.

$ Name = $ form-> form_text ("userid", "userid", "username", "userid ");

$ Passwd = $ form-> form_passwd ("passwd", "passwd", "password", "passwd ");

$ Submit = $ form-> form_button ("", "submit", "submit", "Logon ");

$ Form_item = array ($ name, $ passwd, $ submit );

$ Form-> CreateForm ($ form_item );

?>

</Body>

</Html>

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.