当前位置:Gxlcms > PHP教程 > PHP自动生成表单的Form类

PHP自动生成表单的Form类

时间:2021-07-01 10:21:17 帮助过:62人阅读

\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. 用户登录
    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;//是否使用表格布局
    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="\n";
    16. }else {
    17. $text="\n";
    18. }
    19. return $text;
    20. }
    21. //文本框函数
    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. //密码框函数
    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. //隐藏域函数
    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. //文件域函数
    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. //复选框函数
    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].="";
    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. //单选框函数
    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].="";
    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. //下拉菜单函数
    95. function form_select($id,$name,$options=array(),$selected=false,$label_name,$label_for,$onchange=""){
    96. if($onchange!==""){
    97. $text="\n";
    98. }
    99. foreach ($options as $value=>$key){
    100. if($selected==$value){
    101. $text.="\t\n";
    102. }elseif ($selected===false) {
    103. $text.="\t\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. //多选列表函数
    112. function form_selectmul($id,$name,$size,$options=array(),$label_name,$label_for){
    113. $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. //按钮函数
    119. function form_button($id,$name,$type,$value,$onclick=""){
    120. $text="\n";
    121. if($this->layout==true){
    122. $form_item="
    {$text}
    ";
  • $text.=$form_label;
  • $text.="
  • ";
  • $text.=$form_text;
  • $text.="
  • 自动生成, PHP, Form

    人气教程排行