PHP automatically generate Form class

Source: Internet
Author: User
PHP automatically generate Form class
  • } 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 ();
  • }
  • }
  • ?>


    1. User logon
    2. Require_once ("form. php ");
    3. $ Form = new form ($ _ SERVER ['php _ SELF ']); // submit to this page
    4. $ Form-> layout = false; // no table layout is used. you can comment out this sentence to see if the result is different.
    5. $ Name = $ form-> form_text ("userid", "userid", "username", "userid ");
    6. $ Passwd = $ form-> form_passwd ("passwd", "passwd", "password", "passwd ");
    7. $ Submit = $ form-> form_button ("", "submit", "submit", "logon ");
    8. $ Form_item = array ($ name, $ passwd, $ submit );
    9. $ Form-> CreateForm ($ form_item );
    10. ?>


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

    1. // Form. php
    2. Class form {
    3. Var $ layout = true; // whether to use table layout
    4. Var $ action; // URL to which the form is submitted
    5. Var $ method;
    6. Var $ enctype = "";
    7. Var $ name = "";
    8. Var $ id = "";
    9. Var $ class = "";
    10. Function form ($ action, $ method = "POST") {// initialize the member variable through the constructor
    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 function
    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. // Hide the domain function
    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 domain 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. // Single functions
    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. // Multiple-choice 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.