FuelPHP - 表单编程
FuelPHP 提供了三个类,Form Fieldset,和Input,用于执行表单编程。
Form 类提供了创建所有 HTML 表单元素的选项。
Fieldset 类提供了通过更高级别方法创建 html 元素的选项,集成了模型和验证。
Input 类提供了解析通过 html 表单提交的数据以及 http 参数、服务器变量和用户代理的选项。
在本章中,让我们学习 FuelPHP 中的表单编程。
Form
如前所述,Form 类提供了创建 html 表单元素的方法,重要方法如下−
open()
open() 用于创建新表单。它提供以下两个参数 −
$attributes − 表单标签的属性作为数组或只是操作 URL 作为字符串。
$hidden − 隐藏字段名称及其值的数组。
echo Form::open('/employee/add'); echo Form::open(array('action' => '/employee/add', 'method' => 'post'));
close()
close() 只是关闭表单。
echo Form::close();
input()
input() 创建 html 输入元素。它具有以下三个参数,
$field − 输入元素的名称
$value − 输入元素的值
$attributes −输入元素的属性作为数组
echo Form::input('name', 'jon', array('style' => 'border: 20px;'));
标签元素
label 创建 html 标签元素。它具有以下三个参数,
$label − 要显示的标签
$id − 关联的表单元素 id
$attributes − 标签元素的属性作为数组
echo Form::label('Employee Name', 'employee_name');
hidden
hidden 与输入法类似,不同之处在于它将输入元素的类型设置为 hidden。
password
password 与输入法类似,不同之处在于它将输入元素的类型设置为 password。
radio
radio 与输入法类似,不同之处在于它将输入元素的类型设置为 radio。它有以下四个参数,
$field − 输入元素的名称
$value − 输入元素的值
$checked −该项目是否被选中(true / false)
$attributes − 输入元素的属性作为数组
echo Form::label('Male', 'gender'); echo Form::radio('gender', 'Male', true); echo Form::label('Female', 'gender'); echo Form::radio('gender', 'Female');
checkbox
checkbox 与 input 方法类似,只不过它将输入元素的类型设置为复选框。它有以下四个参数,
$field − 输入元素的名称
$value − 输入元素的值
$checked − 该项目是否被选中(true / false)
$attributes − 输入元素的属性作为数组
echo Form::label('Male', 'gender'); echo Form::checkbox('gender', 'Male', true); echo Form::label('Female', 'gender'); echo Form::checkbox('gender', 'Female');
file
file 与输入法类似,不同之处在于它将输入元素的类型设置为文件。
textarea
textarea 创建 html textarea 元素。它具有以下三个参数,
$field − textarea 元素的名称
$value − textarea 元素的值
$attributes − textarea 元素的属性作为数组
echo Form::textarea ('description', 'original data (value)', array ('rows' => 6, 'cols' => 8));
select
select 创建一个 HTML select 元素。它具有以下四个参数 −
$field − select 元素的名称
$values − 初始选择值
$options − 选项作为数组。可以使用嵌套数组对选项进行分组
$attributes − 输入元素的属性作为数组
echo Form::select ( 'country', 'none', array ( 'none' => 'None', 'asia' => array ( 'in' > 'India', 'cn' => 'China' ), 'us' => 'United States' ) );
submit
submit 与输入法类似,只不过它设置了要提交的输入元素的类型。
button
button 创建 html 按钮元素。它具有以下三个参数,
$field − 按钮元素的名称
$value − 按钮元素的值
$attributes − 按钮元素的属性作为数组
echo Form::button('emp_submit', 'Submit');
reset
reset 与输入法类似,不同之处在于它将输入元素的类型设置为重置。
fieldset_open
fieldset_open 创建 html 字段集和图例元素。它具有以下两个参数 −
attributes − fieldset 元素的属性作为数组
legend − 要创建的图例的名称
// returns <fieldset class = "example-class" id = "example-id"> <legend> Custom Legend </legend> echo Form::fieldset_open (array ( 'class' => 'example-class', 'id' => 'exampleid', 'legend' => 'Custom Legend' ));
fieldset_close
fieldset_close 创建 HTML 字段集关闭标记。
// 返回 </fieldset> echo Form::fieldset_close();
Input 类
Input 类提供读取所有请求数据以及表单详细信息的方法。一些重要方法如下 −
uri
uri 返回请求的当前 URI
// 请求:http://localhost:8080/employee/welcome echo Input::uri(); // 返回 /employee/welcome
method
method 返回请求中使用的 HTTP 方法
echo Input::method() // "POST"
get
get 允许读取 $_GET 变量。它具有以下两个参数,
$index − $_GET 数组的索引
$default − 如果未找到索引,则为默认值。
echo Input::get('age', '20'); // 返回 $_GET['age']
post
post 允许读取 $_POST 变量。它具有以下两个参数,
$index − $_POST 数组的索引
$default − 如果未找到索引,则为默认值
echo Input::get('age', '20'); // 返回 $_POST['age']
param
param 允许从 $_GET、$_POST、$_PUT 或 $_DELETE 变量中获取项目。它具有以下两个参数,
$index − 数组的索引
$default − 如果未找到索引,则为默认值
如果没有指定参数,它将返回所有项目。
echo Input::param('age', '20'); // 返回 $_POST['age']
file
file 允许读取 $_FILE 变量。它具有以下两个参数,
$index − $_POST 数组的索引
$default −如果未找到索引,则为默认值
echo Input::file();
is_ajax
如果请求是通过 AJAX 发出的,则is_ajax 返回 true。
echo Input::is_ajax() // 返回 false
protocol
protocol 返回请求中使用的 HTTP 协议。
echo Input::protocol() // 返回"HTTP"
ip
ip 返回发出请求的 IP 地址。
echo Input::ip() // 返回"84.45.34.24"(公共 IP 地址)
real_ip
real_ip 尝试返回实际发出请求的 IP 地址(如果客户端位于代理后面)。
echo Input::real_ip() // 返回"10.76.12.1"(本地私有 IP 地址)
server
server 允许读取 $_SERVER 变量。它具有以下两个参数,
$index − $_POST 数组的索引
$default − 如果未找到索引,则为默认值。
echo Input::server('HTTP_HOST'); // 返回 localhost:8080
referrer
referrer 从 $_SERVER 变量返回 referrer。这是获取当前请求的 http referrer 的快捷方法。
user_agent
user_agent 从 $_SERVER 变量返回用户代理。这是获取当前请求的 http 用户代理的快捷方法。
query_string
query_string 从 $_SERVER 变量返回查询字符串。这是获取当前请求的查询字符串的快捷方法。
headers
headers 返回特定或所有标头。它具有以下两个参数 −
$index − HTTP 标头的名称
$default − 如果未找到索引,则为默认值。
echo Input::headers('Content-Type'); // returns "text/html"
extension
extension 返回当前请求的 URI 扩展。
// 示例 URL:http://localhost/test/ echo Input::extension(); // NULL // 示例 URL:http://localhost/test.html echo Input::extension(); // 'html'
工作示例
让我们使用 Form 和 Input 类创建一个简单的表单来添加新员工。
创建表单
在员工控制器中创建新操作 get_add,如下所示。
public function get_add() { return Response::forge(View::forge('employee/add')); }
现在,为操作添加视图 fuel/app/views/employee/add.php 如下所示。
<!DOCTYPE html> <html lang = "en"> <head> <title>Employee :: add page</title> <meta charset = "utf-8"> <meta name = "viewport" content = "width = device-width, initial-scale = 1"> <?php echo Asset::css('bootstrap.css'); ?> </head> <body> <div class = "container"> <?php echo Form::open(array('action' => 'employee/add', 'method' => 'post')); ?> <div class = "form-group"> <?php echo Form::label('Employee name:', 'name'); echo Form::input('name', '', array('class' => 'form-control')); ?> </div> <div class = "form-group"> <?php echo Form::label('Employee age:', 'age'); echo Form::input('age', '', array('class' => 'form-control')); ?> </div> <?php echo Form::button('frmbutton', 'Submit', array( 'class' => 'btn btn-default')); ?> <?php echo Form::close(); ?> </div> </body> </html>
这里,我们使用了 bootstrap 来设计表单。FuelPHP 为 bootstrap 组件提供了全面支持。现在,请求页面 http://localhost:8080/employee/add 将显示以下表单。
处理表单
在员工控制器中创建新操作 post_add 来处理表单并将用户输入的员工数据添加到数据库中,如下所示。
public function post_add() { $name = Input::post('name'); $age = Input::post('age'); $model = new model_employee(); $model->name = $name; $model->age = $age; $model->save(); Response::redirect('employee/list'); }
在这里,我们被重定向到员工列表页面,一旦用户输入的数据被保存到数据库中。接下来,我们将创建员工列表页面。
列出员工
创建新操作 action_list 以列出数据库中的员工,如下所示。
public function action_list() { $data = array(); $data['emps'] = model_employee::find('all'); return Response::forge(view::forge('employee/list', $data)); }
为上述操作创建新视图 fuel/app/views/employee/list,如下所示。
<ul> <?php foreach($emps as $emp) { ?> <li><?php echo $emp['name']; ?></li> <?php } ?> </ul>
检查表单
现在,请求 URL,http://localhost:8080/employee/add,输入一些员工数据(如以下屏幕截图所示),然后提交表单。
然后,它会显示数据库中可用的所有员工(包括新添加的员工),如下所示 −