Share on Google+Share on Google+

Zend FrameWork Part-10

In this tutorial we will study about Zend Framwork of PHP and in this current tutorial we will study how to add new books etc.. This is the tenth episode of the series subsequent pages will discuss on other topic like how to delete, edit etc.

Zend Framework-10:

Adding New Books:

Now we will create forms to add new books, There will be two parts to this part:

  • Display a form to add new title, author, and publisher
  • Submit the form data into the database.

Zend_Form component will be used in this tutorial to create the form to add new book details. We will use zf command to create the form:

zf   create form Book

The above command create a new file called Book.php in application/forms. Now copy the following lines and remove the contents of Book.php by it.



class Application_Form_Book extends Zend_Form


public function __construct($options = null){



$id=new Zend_Form_Element_Hidden('id');


$author=new Zend_Form_Element_Text('author');





$publisher=new Zend_Form_Element_Text('publisher');





$title=new Zend_Form_Element_Text('title');





$submit=new Zend_Form_Element_Submit('submit');






public function addAction()


// action body

$this->view->title="Add Any Book";


$form=new Application_Form_Book();











$book=new Application_Model_DbTable_Books();










<?php echo $this->form;?>


In the constructor of Application_Form_Book, we need to  create five form data elements for id, author, publisher, title, and for the submit button respectively. For each individual item we will set attributes, and the label which will have significant meaning for that data item. ZF provides various filter and Int is one of them with the help of this filter we can prevent SQL injection issues.

For the next text elements we will add StripTags and StringTrim filters, which will remove unwanted HTML and whitespaces. Now IndexController's addAction will display the form and take the necessary action to submit the form.


add new book



Posted on: April 19, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null