PHP利用zend Farmework创立一个FORM表单的解决办法
内容摘要
这篇文章主要为大家详细介绍了PHP利用zend Farmework创立一个FORM表单的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随php教程的小玲来看看吧
感兴趣的小伙伴,下面一起跟随php教程的小玲来看看吧
文章正文
这篇文章主要为大家详细介绍了PHP利用zend Farmework创立一个FORM表单的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随php教程的小玲来看看吧!
1.首先让我们设置一下我们的程序,让Zend能够自动载入方法,不需要我们手动的去载入代码如下:
1 2 3 4 5 6 | <code> require_once 'Zend/Loader/Autoloader.php' //载入自动加载类 $loader = Zend_Loader_Autoloader::getInstance(); //自动实例化 $loader ->registerNamespace( 'Application_' ); //注册命名空间(只有系统默认的,和注册的才可以被自动载入) $loader ->registerNamespace( array ( 'Foo_' , 'Bar_' )); //多个命名空间的注册方法 $loader ->setFallbackAutoloader(true); //一个增加消耗的方法,不需要命名空间,直接载入所有类(不被推荐使用) </code> |
代码如下:
1 2 3 4 5 6 7 | <code>set_include_path(implode(PATH_SEPARATOR, array ( realpath (APPLICATION_PATH . '/../library' ), realpath (APPLICATION_PATH . '/forms/' ), get_include_path(), ))); //这里我们包含了我们的forms目录,方便程序的被载入 </code> |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <code><?php /* php教程 www.512Pic.com */ 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, )); } } </code> |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <code><?php /* php教程 www.512Pic.com */ 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 ; //将表单赋值给试图 } } </code> |
代码如下:
1 2 3 4 5 6 7 | <code>Please use the form below to sign our guestbook! <?php /* php教程 www.512Pic.com */ $this ->form->setAction( $this ->url()); echo $this ->form; </code> |
注:关于PHP利用zend Farmework创立一个FORM表单的简单示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释