基础表单生成器

表单分为三部分,分别有三个文件组成,“index.php”,“fountion.php”,“data.php”

data.php是数组文件

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/25
 * Time: 11:03
 */

$data = [
    [
        "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' => 'checkbox',
        'text' => '爱  好:',
        'attr' => ['type' => 'checkbox', 'name' => 'box'],
        'option' => ['m' => '游泳', 'n' => '打游戏','v' => '生活', 'c' => '跑步']
    ],
    [
        'tag' => 'select',
        'text' => '住  址:',
        'attr' => ['name' => 'area'],
        'option' => ['' => '--请选择--', 'BJ' => '北京', 'SH' => '上海', 'SZ' => '深圳',]
    ],
    [
        'tag' => 'textarea',
        'text' => '自我介绍:',
        'attr' => ['name' => 'declare', 'cols' => '50', 'rows' => '5']
    ]
];


fountion.php文件是函数文件

<?php
include "data.php";
//引入文件
function generate(){
    //定义综合函数
    $html="";
    global  $data;
//声明全局变量
    foreach ($data as $item){
        //循环遍历 $item

        if ($item["tag"]=="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);
        }/*else if ($item["tag"]=="checkbox"){
            $html=$html.checkbox_html($item);
        }*/
    }
return $html."<p></p>";
}
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='a' value='{$item['option']['w']}'>男";
$html=$html."<input type= '{$item['attr']['type']}' name='a' value='{$item['option']['m']}'>女";
    }
    return $html."<p></p>";
}
//下拉列表
function select_html($item){
    $html=$item['text'];
    $html=$html."<select>";
    foreach ($item["option"] as $v){
        $html.="<option value='$v'>$v</option>";
    }
    $html=$html."</select>";

return $html."<p></p>";

}
//多行文本框
function textarea_html($item){
    $html="";
    if ($item["attr"]["name"]=="declare"){
        $html=$item["text"]."<textarea name={$item['attr']['name']}></textarea >";
    }
return $html;
}

//复选框


?>

data.php文件是函数文件

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<form action="">
    <?php include "function.php" ;

    echo generate();
    ?>
</form>
</body>
</html>

以上就是表单的内容了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值