Home  >  Article  >  Backend Development  >  PHP automatically generates the Form class of the form

PHP automatically generates the Form class of the form

WBOY
WBOYOriginal
2016-07-23 08:54:571368browse
ntnn";
  • }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. 用户登录
    2. require_once("form.php");
    3. $form=new form($_SERVER['PHP_SELF']); //提交到本页
    4. $form->layout=false; //不使用表格布局,大家可以把这句注释掉看结果有何不同
    5. $name=$form->form_text("userid","userid","用户名","userid");
    6. $passwd=$form->form_passwd("passwd","passwd","密码","passwd");
    7. $submit=$form->form_button("","submit","submit","登录");
    8. $form_item=array($name,$passwd,$submit);
    9. $form->CreateForm($form_item);
    10. ?>
    复制代码

    1. //Form.php
    2. class form {
    3. var $layout=true;//Whether to use table layout
    4. var $action;//The URL to which the form should be submitted
    5. var $method;
    6. var $enctype="";
    7. var $name="";
    8. var $id="";
    9. var $class="";
    10. function form($action,$ method="POST"){ //Initialize member variables through the constructor
    11. $this->action=$action;
    12. $this->method=$method;
    13. }
    14. function form_start(){
    15. $text="
    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. }else {
    40. $text="
    41. n";
    42. }
    43. return $text;
    44. }
    45. //Text box function
    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. //Password box function
    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. //Hidden domain function
    70. function form_hidden($name,$id,$label_name,$label_for,$value=""){
    71. $text="< input type="hidden" name="{$name}" id="{$id}" ";
    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. //File domain function
    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.="tn";
    126. }elseif ($selected===false) {
    127. $text.="tn";
    128. }
    129. }
    130. $text.="";
    131. $label=$this->form_label($label_name,$label_for);
    132. $form_item=$this->form_item($label,$text);
    133. return $form_item;
    134. }
    135. //多选列表函数
    136. function form_selectmul($id,$name,$size,$options=array(),$label_name,$label_for){
    137. $text="n";
    138. $label=$this->form_label($label_name,$label_for);
    139. $form_item=$this->form_item($label,$text);
    140. return $form_item;
    141. }
    142. //按钮函数
    143. function form_button($id,$name,$type,$value,$onclick=""){
    144. $text="n";
    145. if($this->layout==true){
    146. $form_item="
    {$text}
    ";
  • $text.=$form_label;
  • $text.="
  • ";
  • $text.=$form_text;
  • $text.="
  • 自动生成, PHP, Form


    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn