Web表单生成器 练习

例:

在这里插入图片描述
制作如图所示需要三个php(当然一个也行,但是写三个更加规范。)。分别为:

1、data.php

<?php

$data=[
    [
        'text'=>'用  户  名 :',
        'tag'=>'input',
        'attr'=>['type'=>'text','name'=>'username'],
        'placeholder'=>'请设置用户名'
    ],
    [
        'text'=>'手  机  号 :',
        'tag'=>'input',
        'attr'=>['type'=>'text','name'=>'username'],
        'placeholder'=>'可用于登录与找回密码'
    ],
    [
        'text'=>'验  证  码 :',
        'tag'=>'input',
        'attr'=>['type'=>'text','name'=>'username'],
        'placeholder'=>'请输入验证码'
    ],
    [
        'text'=>'密&nbsp&nbsp&nbsp&nbsp码&nbsp&nbsp:',
        'tag'=>'input',
        'attr'=>['type'=>'text','name'=>'username'],
        'placeholder'=>'请设置登录密码'
    ],
    [
        'tag'=>'checkbox',
        'attr'=>['type'=>'checkbox','name'=>'hobby[]'],
        'option'=>['r'=>'阅读并接受《用户协议》'],
    ],
    [
        'tag'=>'submit',
        'attr'=>['type'=>'submit','value'=>'注册'],
    ],
]


?>

2、function.php

<?php
include 'data.php';

function genercate(){
    global $data;
    $html='';
    foreach ($data as $item){
        if ($item['tag']=='input'){
            $html .=input_html($item);
        }else if ($item['tag']=='checkbox'){
            $html .=checkbox_html($item);
        }else if ($item['tag']=='submit'){
            $html .=submit_html($item);
        }
    }
    return $html."<p></p>";
}
//生成input
function input_html($item){
    $html="";
    if ($item['attr']['type']=='text'){
        $html=$item['text']."<input type={$item['attr']['type']} name={$item['attr']['name']} placeholder={$item['placeholder']} style=' height: 18px;'>";
    }

    return $html.'<p></p>';
}
//生成checkbox
function checkbox_html($item){
    $html="<input type={$item['tag']} name={$item['attr']['name']} value={$item['option']['r']} style='    margin-left: 50px;margin-right: 5px;'>{$item['option']['r']} ";
    return $html.'<p></p>';
}

//生成submit
function submit_html($item){
    $html="<input type={$item['tag']} value={$item['attr']['value']} style= 'width: 50px;height: 28px;margin-left: 91px;'>";
    return $html."<p></p>";
}



?>

3、index.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>方式二</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;

        }
        div{
            border: 1px solid deepskyblue;
            height: 300px;
            width: 306px;

            margin: 0 auto;
            margin-top: 100px;
        }
        .p{
            background: deepskyblue;
            color: white;
            padding: 8px;
        }
        form input{
            margin-top: 20px;
        }
        .en[
            margin-left: 128px;
        height: 26px;
        width: 45px;]
    </style>
</head>
<body>
<div>

    <p class="p">注册账户</p>
    <form action="" style="margin-left: 20px;">
        <?php

        include 'function.php';
        echo genercate();

        ?>
    </form>
</div>
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值