Maison  >  Article  >  développement back-end  >  PHP自动生成表单的Form类

PHP自动生成表单的Form类

WBOY
WBOYoriginal
2016-07-23 08:54:571365parcourir
\n\t\n\n";
  • }else {
  • $form_item=$text;
  • }
  • return $form_item;
  • }
  • //文本域函数
  • function form_textarea($id,$name,$cols,$rows,$label_name,$label_for,$value=""){
  • $text="\n";
  • $label=$this->form_label($label_name,$label_for);
  • $form_item=$this->form_item($label,$text);
  • return $form_item;
  • }
  • //文字标签函数
  • function form_label($text,$for){
  • if($for!==""){
  • $label="";
  • }else {
  • $label=$text.":";
  • }
  • return $label;
  • }
  • function form_item($form_label,$form_text){
  • switch ($this->layout){
  • case true:
  • $text="
  • \n";
  • $text.="\t
  • \n";
  • $text.="\t
  • \n";
  • $text.="
  • \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();
  • }
  • }
  • ?>
  • 复制代码
    1. //Form.php
    2. class form {
    3. var $layout=true;//是否使用表格布局
    4. var $action;//表单要提交到的URL
    5. var $method;
    6. var $enctype="";
    7. var $name="";
    8. var $id="";
    9. var $class="";
    10. function form($action,$method="POST"){ //通过构造函数初始化成员变量
    11. $this->action=$action;
    12. $this->method=$method;
    13. }
    14. function form_start(){
    15. $text="
      action}\" method=\"{$this->method}\"";
    16. if($this->class!==""){
    17. $text.=" class=\"{$this->class}\"";
    18. }
    19. if ($this->enctype!=="") {
    20. $text.=" enctype=\"{$this->enctype}\"";
    21. }
    22. if($this->id!==""){
    23. $text.=" id=\"{$this->id}\"";
    24. }
    25. if($this->name!==""){
    26. $text.=" name=\"{$this->name}\"";
    27. }
    28. $text.=">\n";
    29. if($this->layout==true){
    30. $text.="\n";
    31. }
    32. return $text;
    33. }
    34. function form_end(){
    35. if ($this->layout==true) {
    36. $text="\t
    37. \n";
    38. $text.="
    39. \n";
    40. }else {
    41. $text="\n";
    42. }
    43. return $text;
    44. }
    45. //文本框函数
    46. function form_text($name,$id,$label_name,$label_for,$value=""){
    47. $text="
    48. $text.="id=\"{$id}\" ";
    49. if(isset($value)){
    50. $text.="value=\"{$value}\" ";
    51. }
    52. $text.="/>\n";
    53. $label=$this->form_label($label_name,$label_for);
    54. $form_item=$this->form_item($label,$text);
    55. return $form_item;
    56. }
    57. //密码框函数
    58. function form_passwd($name,$id,$label_name,$label_for,$value=""){
    59. $text="
    60. $text.="id=\"{$id}\" ";
    61. if(isset($value)){
    62. $text.="value=\"{$value}\" ";
    63. }
    64. $text.="/>\n";
    65. $label=$this->form_label($label_name,$label_for);
    66. $form_item=$this->form_item($label,$text);
    67. return $form_item;
    68. }
    69. //隐藏域函数
    70. function form_hidden($name,$id,$label_name,$label_for,$value=""){
    71. $text="
    72. if(isset($value)){
    73. $text.="value=\"{$value}\" ";
    74. }
    75. $text.="/>\n";
    76. $label=$this->form_label($label_name,$label_for);
    77. $form_item=$this->form_item($label,$text);
    78. return $form_item;
    79. }
    80. //文件域函数
    81. function form_file($name,$id,$label_name,$label_for,$size=""){
    82. $text="
    83. $text.="id=\"{$id}\" ";
    84. if(isset($size)){
    85. $text.="size=\"{$size}\" ";
    86. }
    87. $text.="/>\n";
    88. $label=$this->form_label($label_name,$label_for);
    89. $form_item=$this->form_item($label,$text);
    90. return $form_item;
    91. }
    92. //复选框函数
    93. function form_checkbox($name,$label=array(),$label_name,$label_for=""){
    94. $i=0;
    95. $text=array();
    96. foreach ($label as $id=>$value){
    97. $text[$i]="";
    98. $text[$i].="";
    99. $i++;
    100. }
    101. $label=$this->form_label($label_name,$label_for);
    102. $form_item=$this->form_item($label,$text);
    103. return $form_item;
    104. }
    105. //单选框函数
    106. function form_radio($name,$label=array(),$label_name,$label_for=""){
    107. $i=0;
    108. $text=array();
    109. foreach ($label as $id=>$value){
    110. $text[$i]="";
    111. $text[$i].="";
    112. $i++;
    113. }
    114. $label=$this->form_label($label_name,$label_for);
    115. $form_item=$this->form_item($label,$text);
    116. return $form_item;
    117. }
    118. //下拉菜单函数
    119. function form_select($id,$name,$options=array(),$selected=false,$label_name,$label_for,$onchange=""){
    120. if($onchange!==""){
    121. $text="\n";
    122. }
    123. foreach ($options as $value=>$key){
    124. if($selected==$value){
    125. $text.="\t\n";
    126. }elseif ($selected===false) {
    127. $text.="\t\n";
    128. }
    129. }
    130. $text.="
    131. ";
    132. $label=$this->form_label($label_name,$label_for);
    133. $form_item=$this->form_item($label,$text);
    134. return $form_item;
    135. }
    136. //多选列表函数
    137. function form_selectmul($id,$name,$size,$options=array(),$label_name,$label_for){
    138. $text="\n";
    139. $label=$this->form_label($label_name,$label_for);
    140. $form_item=$this->form_item($label,$text);
    141. return $form_item;
    142. }
    143. //按钮函数
    144. function form_button($id,$name,$type,$value,$onclick=""){
    145. $text="\n";
    146. if($this->layout==true){
    147. $form_item="
    {$text}
    ";
  • $text.=$form_label;
  • $text.="
  • ";
  • $text.=$form_text;
  • $text.="
  • Déclaration:
    Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn