用php实现一个简易的web表单生成器,Web表单生成器

Web表单生成器

在项目的实际开发中,经常需要设计各种各样表单。直接编写HTML表单虽然简单,但修改、维护相对麻烦。

因此,可以利用PHP实现一个Web表单生成器,使其可以根据具体的需求定制不同功能的表单。具体实现需求如下:

数据的保存形式决定了程序实现的方式。

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

php源代码如下:

1)数据层

$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)编辑层

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']."";

}

else if($item['attr']['type']=='radio'){

//生成单选按钮

$html = $item['text'];

$html = $html."{$item['option']['m']}";

$html = $html."{$item['option']['w']}";

}else if($item['attr']['type']=='password'){

$html = $item['text']."";

}

return $html.'

}

function select_html($item){

$html="";

$html=$item['text'];

$html.="";

foreach ($item['option'] as $v){

$html.="$v";

}

$html.="";

return $html.'

}

function textarea_html($item){

$html="";

$html=$item['text'];

$html.="";

return $html;

}

3)表示层

include "function.php";

echo ent();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值