Web表单生成器

header(“content-type:text/html;charset=utf-8”);//防止乱码

Web表单生成

表单在Web开发中是最基本和常用的功能。在项目的实际开发中,经常需要设计各种各样的表单,直接编写html虽简单,但不利于修改和维护。因此可利用PHP实现一个Web表单生成器。
代码如下:
主要是建立了三个php文件进行引入

  • data.php
<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 19/3/25 025
 * Time: 上午 11:23
 */
$arr=[
    [
        "text" =>"姓     名:",
        "tag" =>"input",
        "attr" =>['type'=>'text','name'=>'username']
    ],
    [
        "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' => 'input',
        'text' => '爱  好:',
        'attr' => ['type' => 'checkbox', 'name' => 'hobby[]'],
        'option' => ['swimming'=>'游泳','reading'=>'读书','running'=>'跑步']
    ],
    [
        'tag' => 'select',
        'text' => '住  址:',
        'attr' => ['name' => 'area'],
        'option' => ['' => '--请选择--', 'BJ' => '北京', 'SH' => '上海', 'SZ' => '深圳','TJ'=>'天津']
    ],
    [
        'tag' => 'textarea',
        'text' => '自我介绍:',
        'attr' => ['name' => 'declare', 'cols' => '50', 'rows' => '5']
    ],
    [
        'tag'=>'input',
        'attr'=>['type'=>'submit','value'=>'提交']
    ]

];

  • function.php
<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 19/3/25 025
 * Time: 上午 11:23
 */
//引入date.php数据
include "date.php";
//综合函数
function generate(){
    global $arr;
    $html="";
    foreach ($arr as $value){
        if($value["tag"]=="input"){
//             生成input标签的函数
                $html.=input_html($value);
        }else if ($value["tag"]=="select"){
//            生成select的函数
            $html.=select_html($value);
        }else if($value["tag"]=="textarea"){
//            生成textarea的函数
            $html.=textarea_html($value);
        }
    }
    return $html;
}
//input函数
function input_html($value){
    $html="";
    //       判断是文本框还是单选按钮
    if ($value["attr"]["type"]=="text"){      //文本框
        $html=$value['text']."<input type='{$value['attr']['type']}' name='{$value['attr']['name']}'>";
    }else if($value["attr"]["type"]=="radio"){       //按钮    单选框
        $html=$value['text'];
        $html.="<input type='{$value['attr']['type']}' name='{$value['attr']['name']}' value='{$value['option']['m']}'>{$value['option']['m']}";
        $html.="<input type='{$value['attr']['type']}' name='{$value['attr']['name']}' value='{$value['option']['w']}'>{$value['option']['w']}";
    }else if($value['attr']['type']=="checkbox"){  //复选框
        $html=$value['text'];
        $html.="<input type='{$value['attr']['type']}' name='{$value['attr']['name']}' value='{$value['option']['swimming']}'>{$value['option']['swimming']}";
        $html.="<input type='{$value['attr']['type']}' name='{$value['attr']['name']}' value='{$value['option']['reading']}'>{$value['option']['reading']}";
        $html.="<input type='{$value['attr']['type']}' name='{$value['attr']['name']}' value='{$value['option']['running']}'>{$value['option']['running']}";
    }else if ($value['attr']['type']=="submit"){
        $html.=" <input type='{$value['attr']['type']}' value='{$value['attr']['value']}'>";
    }
    $html .="<p></p>";
        return $html;
}
//select函数
function select_html($value){
    $html="";
    $html=$value['text'];
    $html.="<select>";
    foreach ($value['option'] as $v){
        $html.="<option value='$v'>$v</option>";
    }
    $html.="</select>";
    $html .="<p></p>";
    return $html;
}
//textares函数
function textarea_html($value){
    $html="";
    $html=$value['text'];
    $html.="<textarea name='{$value['attr']['name']}' cols='{$value['attr']['cols']}' rows='{$value['attr']['rows']}'>";
    $html.="</textarea>";
    $html .="<p></p>";
    return $html;
}

  • inde.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<form action="">
    <?php
    include "function.php";
    echo generate();
    ?>
</form>
</body>
</html>                                                                      

效果如下图在这里插入图片描述

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值