source: trunk/docs/examples/contact_form/contact.php @ 288

Last change on this file since 288 was 288, checked in by quinn, 17 years ago

Organized codebase examples under docs/examples. Added contact form example.

File size: 2.9 KB
Line 
1<?php
2/*
3* contact.php
4* Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information.
5* @author   Quinn Comendant <quinn@strangecode.com>
6* @version  1.0
7* @since    01 Aug 2006 09:35:14
8*/
9
10require_once dirname(__FILE__) . '/_config.inc.php';
11
12
13/********************************************************************
14* CONFIG
15********************************************************************/
16
17require_once 'codebase/lib/FormValidator.inc.php';
18require_once 'codebase/lib/Email.inc.php';
19
20$fv = new FormValidator();
21
22// Set this to the person who should receive emails.
23$email_to = 'The Form Admin <me@example.com>';
24
25
26/********************************************************************
27* MAIN
28********************************************************************/
29
30if (!empty($_POST)) {
31    // Validate submitted data.
32    $fv->notEmpty('name', sprintf(_("%s cannot be blank."), _("Name")));
33    $fv->stringLength('name', 0, 255, sprintf(_("%s must be %d-to-%d characters in length."), _("Name"), 0, 255));
34   
35    $fv->notEmpty('email', sprintf(_("%s cannot be blank."), _("Email")));
36    $fv->stringLength('email', 0, 255, sprintf(_("%s must be %d-to-%d characters in length."), _("Email"), 0, 255));
37    $fv->validateEmail('email');
38   
39    $fv->notEmpty('message', sprintf(_("%s cannot be blank."), _("Message")));
40    $fv->stringLength('message', 0, 32000, sprintf(_("%s must be %d-to-%d characters in length."), _("Message"), 0, 32000));
41       
42    if ($fv->anyErrors()) {
43        // Re-populate form values with the user data so they can make corrections.
44        $frm = resetForm(getFormData());
45    } else {
46        // Setup email object.
47        $email = new Email(array(
48            'to' => $email_to,
49            'from' => sprintf('%s <%s>', $frm['name'], $frm['email']),
50            'subject' => 'Contact form email',
51        ));
52        $email->setTemplate('contact.eml');
53        $email->replace(array(
54            'name' => $frm['name'],
55            'email' => $frm['email'],
56            'message' => $frm['question'],
57        ));
58        // Send email!
59        $email->send();
60
61        // Reset form values to display a blank form.
62        $frm = resetForm();
63        $app->raiseMsg(_("<strong>Thank You!</strong> Your message has been sent."), MSG_SUCCESS, __FILE__, __LINE__);
64    }
65} else {
66    // Reset form values to display a blank form.
67    $frm = resetForm();
68}
69
70
71/********************************************************************
72* OUTPUT
73********************************************************************/
74
75include 'header.ihtml';
76include 'contact.ihtml';
77include 'footer.ihtml';
78
79
80/********************************************************************
81* FUNCTIONS
82********************************************************************/
83
84function resetForm($new_values = array())
85{
86    $default = array(
87        'name' => '',
88        'email' => '',
89        'message' => '',
90    );
91    return array_merge($default, $new_values);
92}
93
94?>
Note: See TracBrowser for help on using the repository browser.