PHP automatically generated form code sharing _ PHP Tutorial

Source: Internet
Author: User
PHP automatically generates form code sharing. PHP automatic form generation code sharing: during PHP development, sometimes forms are not fixed and must be dynamically generated during the program running, here is a PHP automatic creation PHP automatic generation 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

?

} Else {

$ Form_item = $ text;

}

Return $ form_item;

}

// Text domain functions

Function form_textarea ($ id, $ name, $ cols, $ rows, $ label_name, $ label_for, $ value = ""){

$ Text ="{$ Value}\ 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 ="{$ Text }:";

} Else {

$ Label = $ text .":";

}

Return $ label;

}

Function form_item ($ form_label, $ form_text ){

Switch ($ this-> layout ){

Case true:

$ Text ="

$ Text. = "\ t

$ Text. = "\ t

$ Text. ="

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

}

}

?>

\ N \ t \ N \ N "; \ N "; \ N "; \ N "; \ N ";

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

// 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 = "\ n ";

} Else {

$ Text ="\ N ";

}

Return $ text;

}

// Text box function

Function form_text ($ name, $ id, $ label_name, $ label_for, $ value = ""){

$ Text ="

$ 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 ="

$ 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 ="

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 ="

$ 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] ="";

$ Text [$ I]. ="{$ Value}";

$ 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] ="";

$ Text [$ I]. ="{$ Value}";

$ 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 ="\ N "; } Else { $ Text ="\ N ";

}

Foreach ($ options as $ value => $ key ){

If ($ selected = $ value ){

$ Text. = "\ t{$ Key}\ N ";

} Elseif ($ selected === false ){

$ Text. = "\ t{$ Key}\ N ";

}

}

$ Text. ="";

$ 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 ="\ N "; Foreach ($ options as $ value => $ key ){ $ Text. = "\ t{$ Key}\ N "; } $ Text. ="\ 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 ="

If ($ onclick! = ""){

$ Text. = "onclick = '{$ onclick }'";

}

$ Text. = ">". $ value;

$ Text. ="

\ N ";

If ($ this-> layout = true ){

$ Form_item ="

{$ Text}
";

$ Text. = $ form_label;

$ Text. ="

";

$ Text. = $ form_text;

$ Text. ="

Example:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

User logon

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

?>

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

In the PHP development process, 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 PHP automatic creation...

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.