国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

解析在zend Farmework下如何創立一個FORM表單

2020-03-22 18:24:27
字體:
來源:轉載
供稿:網友
1.首先讓我們設置一下我們的程序,讓Zend能夠自動載入方法,不需要我們手動的去載入
復制代碼 代碼如下:
require_once 'Zend/Loader/Autoloader.php' //載入自動加載類
$loader = Zend_Loader_Autoloader::getInstance();//自動實例化
$loader- registerNamespace('Application_');//注冊命名空間(只有系統默認的,和注冊的才可以被自動載入)
$loader- registerNamespace(array('Foo_', 'Bar_')); //多個命名空間的注冊方法
$loader- setFallbackAutoloader(true); //一個增加消耗的方法,不需要命名空間,直接載入所有類(不被推薦使用)

然后請注意,你的包含目錄是否已經包含了,你自己的需被載入的目錄
復制代碼 代碼如下:
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
realpath(APPLICATION_PATH . '/forms/'),
get_include_path(),
)));
//這里我們包含了我們的forms目錄,方便程序的被載入

2.確認下form的目錄
在application/forms/下 建立一個 Guestbook.phps
作為我們form的類文件,如下:
復制代碼 代碼如下:
?php
html' target='_blank'>class Application_Form_Guestbook extends Zend_Form
{
public function init()
{
// Set the method for the display form to POST
$this- setMethod('post');//設置提交方式

// Add an email element
$this- addElement('text', 'email', array(//原件的類型,名詞,和一些其他信息的定義
'label' = 'Your email address:',
'required' = true,
'filters' = array('StringTrim'),
'validators' = array(
'EmailAddress',
)
));

// Add the comment element
$this- addElement('textarea', 'comment', array(
'label' = 'Please Comment:',
'required' = true,
'validators' = array(
array('validator' = 'StringLength', 'options' = array(0, 20))
)
));

// Add a captcha
$this- addElement('captcha', 'captcha', array(
'label' = 'Please enter the 5 letters displayed below:',
'required' = true,
'captcha' = array(
'captcha' = 'Figlet',
'wordLen' = 5,
'timeout' = 300
)
));

// Add the submit button
$this- addElement('submit', 'submit', array(
'ignore' = true,
'label' = 'Sign Guestbook',
));

// And finally add some CSRF protection
$this- addElement('hash', 'csrf', array(
'ignore' = true,
));
}
}

然后添加一個路由控制文件
applictaion/controller/GuestbookController.php
復制代碼 代碼如下:
?php
class GuestbookController extends Zend_Controller_Action
{
// snipping indexAction()...
public function signAction()
{
$request = $this- getRequest();//獲取接受到得信息
// include_once("../application/forms/Guestbook.php"); 手動加載類,只有不能自動載入時,才需要
$form = new Application_Form_Guestbook;//實例化這個方法

if ($this- getRequest()- isPost()) {//如果是POST傳遞的結果
if ($form- isValid($request- getPost())) {//判斷傳遞是否有效
$comment = new Application_Model_Guestbook($form- getValues());
$mapper = new Application_Model_GuestbookMapper();
$mapper- save($comment);
return $this- _helper- redirector('index');
}
}

$this- view- form = $form;//將表單賦值給試圖
}
}

最后添加一個簡單的sign視圖文件即可:
地址:application/views/scripts/guestbook/sgin.php
復制代碼 代碼如下:
Please use the form below to sign our guestbook!
?php
$this- form- setAction($this- url());
echo $this- form;

PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贵阳市| 灵山县| 弥勒县| 台东县| 自治县| 体育| 高青县| 隆尧县| 东丽区| 文山县| 卢湾区| 和平县| 浮山县| 镶黄旗| 黄浦区| 肇源县| 连城县| 定边县| 富阳市| 清水河县| 邮箱| 长顺县| 永丰县| 西贡区| 仁化县| 卫辉市| 云阳县| 禄劝| 泗阳县| 双鸭山市| 蕲春县| 肇庆市| 阿坝县| 延边| 乌兰县| 乐安县| 冷水江市| 兴隆县| 淳安县| 洪洞县| 金平|