Minimal feedback form with ZF

October 4th, 2009 by raspi Leave a reply »

Here’s minimal feedback form example using Zend Framework 1.9.2 without MVC.

<?php
ignore_user_abort(true);
header("Content-Type: text/html; charset=utf-8")

error_reporting(E_ALL);
ini_set('display_errors', '1');

ini_set('magic_quotes_gpc', false);
ini_set('magic_quotes_runtime', false);

ini_set('default_charset', 'UTF-8');

ini_set('iconv.input_encoding', 'UTF-8');
ini_set('iconv.output_encoding', 'UTF-8');
ini_set('iconv.internal_encoding', 'UTF-8');

function stripinputslashes(&$input)
{
  if (is_array($input))
  {
    foreach ($input as $key => $value)
    {
      switch (gettype($value))
      {
        default: break;
        case 'string':
          $input[$key] = stripinputslashes($value);
        break;
      } // /switch
    } // /foreach
  } // /if
  else
  {
    switch (gettype($input))
    {
      default: break;
      case 'string':
        $input = stripslashes($input);
      break;
    } // /switch
  } // /else

  return true;
} // /function

if (version_compare(phpversion(), 6) === -1)
{
  if (function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc() === 1)
  {
    array_walk_recursive($_GET, 'stripinputslashes');
    array_walk_recursive($_REQUEST, 'stripinputslashes');

    if (isset($_POST))
    {
      array_walk_recursive($_POST, 'stripinputslashes');
    } // /if

  } // /if
} // /if

// Add library path
set_include_path(implode(PATH_SEPARATOR, array(
  realpath(dirname(__FILE__) . '/../library'),
  get_include_path(),
)));

// Enable autoloader
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

$view = new Zend_View();

$form = new Zend_Form();
$form->setView($view);
$form->setMethod(Zend_Form::METHOD_POST);
$form->setAction('/');

$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('OK');

$email = new Zend_Form_Element_Text('email');
$email->setRequired(true);
$email->setLabel('E-mail');
$email->addFilter('StringTrim');
$email->addFilter('StringToLower');
$email->addValidator('StringLength', false, array(7));
$email->addValidator(new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_DNS, true));

$txt = new Zend_Form_Element_Textarea('text');
$txt->setRequired(true);
$txt->addFilter('StringTrim');
$txt->addValidator('StringLength', false, array(3));

// Add fields to form
$form->addElement($email);
$form->addElement($txt);
$form->addElement($submit);

if (isset($_POST) && !empty($_POST))
{
  if ($form->isValid($_POST))
  {
    $values = $form->getValues();

    $m = new Zend_Mail('UTF-8');
    $m->setBodyText($values['text']);
    $m->setFrom($values['email']);
    $m->addTo('feedback@example.com');
    $m->setSubject('Feedback');
    $m->send();

    header("Location: /ok.php");
  }

}

// Output form to page
echo $form;

Page contains e-mail and free text field. E-mail address is checked against DNS. Form contents are send to feedback@example.com and given e-mail address is the sender. stripinputslashes kills all stupid slashes from _POST data.

Directory structure:

/home/foo/public_html/index.php (feedback form)
/home/foo/public_html/ok.php
/home/foo/library/Zend/ (ZF files)

ok.php contains something like “Thank you for giving feedback.”

Advertisement

Leave a Reply