<nephtali>

A simple, flexible, fast, and security-focused PHP framework
Nephtali documentation

Contact form

A basic contact form that includes field validation, error handling, and feedback upon success. View the contact form (redux) example to see how to progressively enhance the form by leveraging the REST-ful API automatically available on Nephtali pages.

Code

Markup in contact/index.php

<h2>Send us an email</h2>
<div id="status">
<!--pipe:mail--> 
<!--view:status-->
<div class="status_update">
   <!--data--><h3>{message}</h3><!--data-->
</div>
<!--view:status-->
<!--view:error-->
<div class="status_error">
   <h3>Whoops!</h3>
   <p>Sorry, the message wasn't sent.</p>
</div>
<!--view:error-->
<!--view:feedback-->
<div class="status_error">
   <h3>Some contact info was invalid:</h3>
   <ul class="standard_list">
   <!--data-->
      <li>{message}</li>
   <!--data-->
   </ul>
</div>
<!--view:feedback-->
<!--pipe:mail-->
</div>
<form action="/contact/" method="post">
   <ul>
      <li>
         <label for="name">Name</label>
         <input type="text" id="name" name="name" placeholder="First and last name" />
      </li>
      <li>
         <label for="email">Email</label>
         <input type="text" id="email" name="email" placeholder="example@site.com" />
      </li>
      <li>
         <label for="message">Message</label>
         <textarea id="message" name="message" rows="2" cols="60" placeholder="Your message">
         </textarea>
      </li>
      <li>
         <input type="submit" value="Send" />
      </li>
   </ul>
</form>

PHP in nsite/contact/index.php

// register ports.
n\port\register_bulk(
   $ports = array(
      'name' => array(
         'max_length' => 50,
         'filter' => n\constant\FILTER_TEXT
      ),
      'email' => array(
         'max_length' => 250,
         'filter' => n\constant\FILTER_EMAIL
      ),
      'message' => array(
         'max_length' => 600,
         'filter' => n\constant\FILTER_TEXT_MULTILINE
      )
   )
);
// register pure action pipe.
n\pipe\register_pure_action($name = 'mail', $actions = array(
   'send' => array(
      'io_func' => function($port_vals){
         // use Zend's really nice email class
         include('Zend/Mail.php');
         include('Zend/Mail/Transport/Smtp.php');
         $message = "Name: ".$port_vals['name']."\n"."Email: ".$port_vals['email']."\n\n".$port_vals['message'];
         $tr = new Zend_Mail_Transport_Smtp($server = 'smtp.gmail.com', $config = array(
            'auth' => 'login',
            // set in nconfig.php
            'username' => n\config\get('acc'),
            'password' => n\config\get('pw'),
            'port' => 587,
            'ssl' => 'tls'
         ));
         Zend_Mail::setDefaultTransport($tr);
         $mail = new Zend_Mail();
         $mail->setBodyText($message)
            ->setFrom('address@website.com')
            ->addTo('contact@website.com')
            ->setSubject('Contact form submission')
            ->send();
         return true;
      },
      'signature' => array('name','email','message'),
      'message' => 'Thank you for contacting us!'
   )
));

Output for standard view (i.e., form not submitted)

<div id="canvas">
<h2>Send us an email</h2>
<form action="/contact/" method="post">
…

Output for submitted form with invalid name

<div class="status_error">
   <h3>Some contact info was invalid:</h3>
   <ul class="standard_list">
      <li>'Name' is a required field.</li>
   </ul>
</div>
<div id="canvas">
<h2>Send us an email</h2>
<form action="/contact/" method="post">
…

Output for successful submission

<div class="status_update">
   <h3>Thank you for the message</h3>
   <p>We appreciate your feedback.</p>
</div>
<div id="canvas">
<h2>Send us an email</h2>
<form action="/contact/" method="post">
…
blog comments powered by Disqus