Making a simple form in Zend framework

  • Firstally define the directory structure of zend framework.
  • Create index.php file inside the project/ directory and write the following code:
<?php
define('ROOT_DIR', dirname(__FILE__));
set_include_path('.'
. PATH_SEPARATOR . ROOT_DIR . '/library'
. PATH_SEPARATOR . ROOT_DIR . '/application/models'
. PATH_SEPARATOR . ROOT_DIR . '/application/forms'
. PATH_SEPARATOR . get_include_path()
);
require_once "Zend/Loader/Autoloader.php";
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
$config = new Zend_Config_Ini(ROOT_DIR.'/application/config.ini', 'general');
$db = Zend_Db::factory($config->db);
Zend_Db_Table::setDefaultAdapter($db);
$frontController = Zend_Controller_Front::getInstance();
$frontController->throwExceptions(true);
$frontController->setControllerDirectory(ROOT_DIR.'/application/controllers');
$frontController->dispatch();
?>
  • Now create a form named CustomForm.php inside project/application/forms/ directory and write the following code to create a form for Data Entry:
<?php
class CustomForm extends Zend_Form
{
public function init()
{
    $this->setMethod('post');
    $id = $this->createElement('hidden','id'); 
    $username = new Zend_Form_Element_Text('username');
    $username->setLabel('Username:')
    ->setAttrib('maxlength',75);
    $firstname = $this->createElement('text','firstname');
    $firstname->setLabel('FirstName:')
    ->setAttrib('maxlength',75);
    $lastname = $this->createElement('text','lastname');
    $lastname->setLabel('LastName:')
    ->setAttrib('maxlength',75);
    $email = $this->createElement('text','email');
    $email->setLabel('Email:')
    ->setAttrib('maxlength',75);        
    $comment = new Zend_Form_Element_Textarea('comment');
    $comment->setLabel('Comment:')
    ->setOptions(array(
    'id' => 'comment',
    'rows' => '5',
    'cols' => '30',
    )); 
    $password = $this->createElement('password','password');
    $password->setLabel('password:')
    ->setAttrib('maxlength',75);    
    $captcha = new Zend_Form_Element_Captcha('captcha', array(
    'captcha' => array(
    'captcha' => 'Figlet',
    'wordLen' => 4,
    'timeout' => 200,
    )
    ));
    $captcha->setLabel('Verification:');  
    $signin = $this->createElement('submit','signin');
    $signin->setLabel("sign Up")
    ->setIgnore(true);
    $reset = new Zend_Form_Element_Reset('reset');
    $reset->setLabel('Cancel');  
    $this->addElements(array(
    $username,
    $firstname,
    $lastname,
    $email,
    $password,
    $captcha,
    $signin,
    $id,
   $reset
));
}}
?>
  • Create a Database and note down the name you created. Create table by executing the following query :
 CREATE TABLE IF NOT EXISTS `users` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `username` varchar(75) NOT NULL,
 `firstname` varchar(75) NOT NULL,
 `lastname` varchar(75) NOT NULL,
 `email` varchar(15) NOT NULL,
 `password` varchar(75) NOT NULL,
  'captcha' varchar(75) NOT NULL,
PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
  • Now configure the database settings. Create a file config.ini inside the /project/application/ directory, write the following code, fill in your details and save the file.
[general]
 db.adapter = "PDO_MYSQL"
 db.params.host = "localhost"
 db.params.username = "root"
 db.params.password = ""
 db.params.dbname = "project"
  • Create the IndexController.php inside project/application/controllers/ and write the following code in it:
<?php
class IndexController extends Zend_Controller_Action
{
        public function addAction()
   {
       $users  = new Users();
       $form = new CustomForm();
       $this->view->form = $form;
       if ($this->getRequest()->isPost()) 
    {
           $formData  = $this->_request->getPost();
           if ($form->isValid($formData)) 
        {
           unset($formData['signin']);
           $users->insert($formData);
       echo "your data has been successfully stored";
               }
       }  }} ?>
  • We have to setup a model for the Database table which we are using, which is also used in indexAction() of IndexController.php file so, create a file Users.php which resides in /project/application/models/ directory and write the corresponding code.y
<?php class Users extends Zend_Db_Table {    protected $_name = “users”; } ?>
  • Create add.phtml file in project/application/views/scripts/index/ and write the following code in it.
    <center><h4>Contact Form</h4>
    <?php
     echo $this->form;
    ?></center>
  • Now type in browser : http://localhost/project/index/add
This entry was posted in Uncategorized. Bookmark the permalink.

1 Response to Making a simple form in Zend framework

  1. Elizabeth says:

    your website is very stylish and modern. and your articles are all informative.http://www.callacc.com

Leave a comment