drw / php-form
此表单用于使用PHP生成HTML表单
This package is auto-updated.
Last update: 2024-09-16 02:46:50 UTC
README
作为一名网页程序员,你知道表单是网络的核心部分(实际上,网络引擎,我们几乎所有的操作都是基于表单)。现在,为了创建一个不易被黑客攻击的完整表单,你需要包括 csrftoken,转义以防止 XXS攻击,以及处理 一对多关系 的潜在复杂性问题,这真的太繁琐了,每次都要从头编写代码,而且,如果你忘记了 csrftoken 或对 htmlspecialchars 的调用,你将有一个安全漏洞。因此,我创建了此 表单插件,它包含许多表单可以包含的元素的方法,你可以根据需要轻松添加额外的功能。
用法 安装插件 你可以通过以下两种方式安装插件:
- composer
composer require drw/php-form(推荐) - 你可以下载包,解压缩它,并包含或要求它
支持的HTML输入类型 支持的输入类型包括 form start:开始表单并输出开头的 <form ...> 标签,有两个可选参数。第一个是一个数组,其中包含要显示的值,按字段名称索引。通常,它是来自另一个表单提交的 $_POST 数组或从数据库检索的行。第二个参数是动作,但几乎总是希望回到同一个文件。csrftoken 放置在每一个表单中。
form end 完成表单。文本方法:text($field, $label/*可以是 null*/, $len/*默认是50*/, $placeholder/*也可以是 null*/, $break/*默认是 true*/, $password/*这表示字段是密码还是文本*/, $value )
标签方法:label($field, $label/*标签可以是 null*/, $break/*break 可以是 true 或 false*/)
按钮方法:button($field, $label/*标签可以是 null*/, $break/*默认是 true 但也可以是 false)
hspace 方法:hspace(/*它不接收参数*/)
复选框方法:checkbox($field, $label/*此参数也可以是 null*/, $break/*break 可以是 true 或 false*/)
单选按钮方法:radio($field, $label/*此参数也可以是 null*/, $value, $break/*break 可以是 true 或 false*/)
日期插件 date($field, $label/*此参数也可以是 null*/, $break/*break 可以是 true 或 false*/)
以下是如何使用它的示例。
require __DIR__ . '/path/to/file';// 假设你使用第二种方法使用此插件,
$f = new Form();// Start the form
$f->start($row);// It could be null(if the form is new),but if it is prepopulated(then you can include like the example)
$f->hidden('id', $row['id'] ?? '');// The hidden method equates to the hidden input form
$f->text('last', 'Last Name:', 30, 'Last Name');// The text method equates the text input method
$f->text('first', 'First:', 20, 'First Name', false);
$f->text('street', 'Street:', 50, 'Street');
$f->text('city', 'City:', 20, 'City');
$f->text('state', 'State:', 10, 'State', false);
//$f->foreign_key('specialty_id', 'name', 'Specialty');
$f->radio('billing', 'Monthly', 'month');
$f->hspace(2);// Use to add 2 horizontal spaces to the form
$f->radio('billing', 'Yearly', 'year', false);
$f->hspace(2)// Use to add 2 horizontal spaces to the form;
$f->radio('billing', 'Recurring', 'recurring', false);
$f->menu('contact', 'Contact:',
array('phone', 'email', 'mail', 'none'), true, 'email');// This is use to add drop down menu
$f->checkbox('premium', 'Premium:', false);// THis is use to add checkbox to the form
$f->date('since', 'Member Since:', false); // This is use to add dater to the form
$f->button('action_save', 'Save');// This is use to add button to the form
$f->end();// This is use to end the form