Form class for PHP to automatically generate forms

Source: Internet
Author: User
  • $text. = "\ t
  • $text. = "\ t
  • $text. = "
  • ?>Copy Code
      1. User Login
      2. !--? php
      3. require_once ("form.php");
      4. $form =new form ($_server[' php_self ');//submit to this page
      5. $form->layout=false;//Do not use table layout, everyone You can use this comment to see what the difference is.
      6. $name = $form->form_text ("userid", "userid", "username", "userid");
      7. $passwd = $form->form_passwd ("passwd", "passwd", "Password", "passwd");
      8. $submit = $form->form_button ("", "submit", "Submit", "Login");
      9. $form _item=array ($name, $passwd, $submit);
      10. $form->createform ($form _item);
      11. ?
    Copy Code
  • \n\t \ n \ n ";
  • }else {
  • $form _item= $text;
  • }
  • return $form _item;
  • }
  • Text field 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 Label function
  • function Form_label ($text, $for) {
  • if ($for!== "") {
  • $label = "{$text}:";
  • }else {
  • $label = $text. " :";
  • }
  • return $label;
  • }
  • function Form_item ($form _label, $form _text) {
  • Switch ($this->layout) {
  • Case true:
  • $text = "
  • \ n "; \ n "; \ n "; \ 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;//whether to use table layout
    4. var $action;//URL to which the form is to 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 variable with constructor function
    11. $this->action= $action;
    12. $this->method= $method;
    13. }
    14. function Form_start () {
    15. $text = "\ n";
    16. }else {
    17. $text = "\ n";
    18. }
    19. return $text;
    20. }
    21. text box functions
    22. function Form_text ($name, $id, $label _name, $label _for, $value = "") {
    23. $text = "
    24. $text. = "Id=\" {$id}\ "";
    25. if (Isset ($value)) {
    26. $text. = "Value=\" {$value}\ "";
    27. }
    28. $text. = "/>\n";
    29. $label = $this->form_label ($label _name, $label _for);
    30. $form _item= $this->form_item ($label, $text);
    31. return $form _item;
    32. }
    33. Password box function
    34. function form_passwd ($name, $id, $label _name, $label _for, $value = "") {
    35. $text = "
    36. $text. = "Id=\" {$id}\ "";
    37. if (Isset ($value)) {
    38. $text. = "Value=\" {$value}\ "";
    39. }
    40. $text. = "/>\n";
    41. $label = $this->form_label ($label _name, $label _for);
    42. $form _item= $this->form_item ($label, $text);
    43. return $form _item;
    44. }
    45. hidden field functions
    46. function Form_hidden ($name, $id, $label _name, $label _for, $value = "") {
    47. $text = "
    48. if (Isset ($value)) {
    49. $text. = "Value=\" {$value}\ "";
    50. }
    51. $text. = "/>\n";
    52. $label = $this->form_label ($label _name, $label _for);
    53. $form _item= $this->form_item ($label, $text);
    54. return $form _item;
    55. }
    56. File field functions
    57. function Form_file ($name, $id, $label _name, $label _for, $size = "") {
    58. $text = "
    59. $text. = "Id=\" {$id}\ "";
    60. if (Isset ($size)) {
    61. $text. = "Size=\" {$size}\ "";
    62. }
    63. $text. = "/>\n";
    64. $label = $this->form_label ($label _name, $label _for);
    65. $form _item= $this->form_item ($label, $text);
    66. return $form _item;
    67. }
    68. check box function
    69. function Form_checkbox ($name, $label =array (), $label _name, $label _for= "") {
    70. $i = 0;
    71. $text =array ();
    72. foreach ($label as $id = = $value) {
    73. $text [$i]=]";
    74. $text [$i].= "{$value}";
    75. $i + +;
    76. }
    77. $label = $this->form_label ($label _name, $label _for);
    78. $form _item= $this->form_item ($label, $text);
    79. return $form _item;
    80. }
    81. Radio Box function
    82. function Form_radio ($name, $label =array (), $label _name, $label _for= "") {
    83. $i = 0;
    84. $text =array ();
    85. foreach ($label as $id = = $value) {
    86. $text [$i]=]";
    87. $text [$i].= "{$value}";
    88. $i + +;
    89. }
    90. $label = $this->form_label ($label _name, $label _for);
    91. $form _item= $this->form_item ($label, $text);
    92. return $form _item;
    93. }
    94. Drop-down menu function
    95. function Form_select ($id, $name, $options =array (), $selected =false, $label _name, $label _for, $onchange = "") {
    96. if ($onchange!== "") {
    97. $text = "\ n "; } else{$text = "\ n ";
    98. }
    99. foreach ($options as $value = = $key) {
    100. if ($selected = = $value) {
    101. $text. = "\ t{$key}\n ";
    102. }elseif ($selected ===false) {
    103. $text. = "\ t{$key}\n ";
    104. }
    105. }
    106. $text. = "";
    107. $label = $this->form_label ($label _name, $label _for);
    108. $form _item= $this->form_item ($label, $text);
    109. return $form _item;
    110. }
    111. Multi-select list function
    112. function Form_selectmul ($id, $name, $size, $options =array (), $label _name, $label _for) {
    113. $text = "\ n "; foreach ($options as $value = = $key) {$text. = "\ t{$key}\n "; } $text. = "\ n ";
    114. $label = $this->form_label ($label _name, $label _for);
    115. $form _item= $this->form_item ($label, $text);
    116. return $form _item;
    117. }
    118. button function
    119. function Form_button ($id, $name, $type, $value, $onclick = "") {
    120. $text = "
    121. if ($onclick!== "") {
    122. $text. = "onclick= ' {$onclick} '";
    123. }
    124. $text. = ">". $value;
    125. $text. = "
    126. \ n ";
    127. if ($this->layout==true) {
    128. $form _item= "
    {$text}
    ";
  • $text. = $form _label;
  • $text. = "
  • ";
  • $text. = $form _text;
  • $text. = "
  • Automatic generation, PHP, Form
  • 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.