Web表单生成器


  在项目的实际开发中,经常需要设计各种各样表单。直接编写HTML表单虽然简单,但修改、维护相对麻烦。
   因此,可以利用PHP实现一个Web表单生成器,使其可以根据具体的需求定制不同功能的表单。具体实现需求如下:

  数据的保存形式决定了程序实现的方式。
  因此,根据上述开发要求,可以将每个表单项作为一个数组元素,每个元素利用一个关联数组描述,分别为:标记tag、提示文本text、属性数组attr、选项数组option和默认值default。

php源代码如下:

1)数据层

<?php
$date = [
    [
        "text" =>"姓 名:",
        "tag" =>"input",
        "attr" =>['type'=>'text','name'=>'username']
    ],
    [
        "text" =>"账 号:",
        "tag" =>"input",
        "attr" =>['type'=>'text','name'=>'username']
    ],
    [
        "text" =>"密 码:",
        "tag" =>"input",
        "attr" =>['type'=>'password','name'=>'pwd']
    ],
    [
        "text" =>"邮 箱:",
        "tag" =>"input",
        "attr" =>['type'=>'text','name'=>'email']
    ],
    [
        "text" =>"电 话:",
        "tag" =>"input",
        "attr" =>['type'=>'text','name'=>'tel']
    ],
    [
        'tag' => 'input',
        'text' => '性  别:',
        'attr' => ['type' => 'radio', 'name' => 'gender'],
        'option' => ['m' => '男', 'w' => '女']
    ],
    [
        'tag' => 'select',
        'text' => '住  址:',
        'attr' => ['name' => 'area'],
        'option' => [
            '' => '--请选择--',
            'BJ' => '北京',
            'SH' => '上海',
            'SZ' => '深圳',
            'CQ' => '重庆',
            'TJ' => '天津',
            'HB' => '河北',
            'SD' => '山东',
            'LN' => '辽宁',
            'HLJ' => '黑龙江',
            'JL' => '吉林',
            'GS' => '甘肃',
            'QH' => '青海',

            ]
    ],
    [
        'tag' => 'textarea',
        'text' => '自我介绍:',
        'attr' => ['name' => 'declare', 'cols' => '50', 'rows' => '5']
    ]
];
?>

2)编辑层

<?php
include "date.php";
   function  ent(){
       global $date;
       $html= "";
       foreach ($date as $item){
           if($item['tag']=='input'){
               //生成input标签函数
              $html=$html.input_html($item);
           }
           else if($item['tag']=='select'){
               //生成下拉列表标签函数
               $html=$html.select_html($item);
           }
           else if($item['tag']=='textarea'){
               //生成多行文本
               $html=$html.textarea_html($item);
           }
       }
       return $html;
   }
   function input_html($item){
       $html = "";
       if($item['attr']['type']=='text'){
           //生成文本框
           $html = $item['text']."<input type='{$item['attr']['type']}' name='{$item['attr']['name']}'>";
       }
       else if($item['attr']['type']=='radio'){
           //生成单选按钮

           $html = $item['text'];
           $html = $html."<input type='{$item['attr']['type']}' name='{$item['attr']['name']}' value='{$item['option']['m']}'>{$item['option']['m']}";
           $html = $html."<input type='{$item['attr']['type']}' name='{$item['attr']['name']}' value='{$item['option']['w']}'>{$item['option']['w']}";
       }else if($item['attr']['type']=='password'){
           $html = $item['text']."<input type='{$item['attr']['type']}' name='{$item['attr']['name']}'>";
       }
       return $html.'<p></p>';
   }
   function select_html($item){
       $html="";
       $html=$item['text'];
       $html.="<select>";
       foreach ($item['option'] as $v){
           $html.="<option value='$v'>$v</option>";
       }
       $html.="</select>";
       return $html.'<p></p>';
   }
   function textarea_html($item){
       $html="";
       $html=$item['text'];
       $html.="<textarea cols='{$item['attr']['cols']}' rows='{$item['attr']['rows']}'></textarea>";
       return $html;
   }

3)表示层

<?php
include "function.php";
echo ent();

更多推荐

Web表单生成器