The Contact Page: The Action Handler
Monday, November 2, 2009
The visual implementation of the contact page is similar to that of the home page, as described in the post The Home Page. However, it also makes use of Zend_Form and Zend_Validate to render and process the contact form, and Zend_Layout, Zend_Filter and Zend_Mail to render and send the contact e-mail to the site admin.
As the implementation is complex and is spread over a number of files and classes, I shall be looking at it over the course of several posts. In this post I shall be concentrating on the action handler.
The action handler starts by retrieving the request object:
public function contactAction() { $request = $this->getRequest();
This will be used to determine whether the user has submitted the contact form and what data was submitted.
Then the function creates the form object:
$form = new Default_Form_Contact();
We shall look at the implementation of this class in a later post.
Then the function determines whether the user has posted the form:
if ($request->isPost()) {
If the user has posted the form, the function retrieves the data posted and validates it against the form:
$post = $request->getPost(); if ($form->isValid($post)) {
If the data is valid, the function then creates and sends the e-mail.
The first step is to create a Zend_Layout object, then set the layout script and its location:
try
{
$layout = new Zend_Layout();
$layout->setLayoutPath(APPLICATION_PATH . "/layouts/scripts");
$layout->setLayout('contact-email');The Zend_Layout will now look for a file called contact-email.phtml in the application/layouts/scripts directory.
The next step is to create a filter and use it to eliminate any possible HTML injection, before passing the parameters to the layout script:
$htmlEntities = new Zend_Filter_HtmlEntities(); $layout->name = $htmlEntities->filter($post['name']); $layout->email = $htmlEntities->filter($post['email']); $layout->message = $htmlEntities->filter($post['message']);
Then the layout is rendered and stored in a variable:
$bodyHtml = $layout->render();
The sending of the mail is handled by the Default_Model_Mail_Contact class, which will be examined in a later post. The function creates an object of that class and call its send method, passing the HTML generated above as the parameter:
$mail = new Default_Model_Mail_Contact(); $mail->send($bodyHtml);
Then a variable is passed to the view script to flag that the mail has been sent:
$this->view->sent = true; }
Any exceptions thrown during the process of sending the mail are handled in the following catch block:
catch (Exception $e) { $this->view->errorMessage = $e->getMessage(); } }
If the form validation fails, the error is passed in a variable to the view script:
else { $this->view->error = $form->getFirstFormError(); } }
Finally, the form object is always passed to the view script, to allow it to be rendered if necessary.
$this->view->form = $form; }
The layout script for the e-mail is quite simple:
<div> <div><strong>Name:</strong> <?php echo $this->layout()->name; ?></div> <div> <strong>E-mail:</strong> <a href="mailto:<?php echo $this->layout()->email; ?>"> <?php echo $this->layout()->email; ?></a> </div> <div><strong>Message:</strong> <?php echo $this->layout()->message; ?></div> </div>
The fields submitted by the user are injected into the appropriate places in the e-mail.
In the next post, I shall look at the view script for the contact page.

Practical Web 2.0 Applications with PHP (Expert's Voice) by Quentin Zervaas
Beginning Databases with PostreSQL: From Expert to Professional 2nd Edition: From Novice to Professional by Neil Matthew, Richard Stones