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

Last change on this file since 413 was 413, checked in by anonymous, 12 years ago

Added header and footer to docs/examples. Updated message printing methods to include before, after, and gotohash options. Minor other fixes.

File size: 3.8 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2012 Strangecode, LLC
6 *
7 * This file is part of The Strangecode Codebase.
8 *
9 * The Strangecode Codebase is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * The Strangecode Codebase is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24* contact.php
25*
26* @author   Quinn Comendant <quinn@strangecode.com>
27* @version  1.0
28* @since    01 Aug 2006 09:35:14
29*/
30
31require_once dirname(__FILE__) . '/_config.inc.php';
32
33
34/********************************************************************
35* CONFIG
36********************************************************************/
37
38require_once 'codebase/lib/FormValidator.inc.php';
39require_once 'codebase/lib/Email.inc.php';
40
41$fv = new FormValidator();
42
43// Set this to the person who should receive emails.
44$email_to = 'The Form Admin <me@example.com>';
45
46
47/********************************************************************
48* MAIN
49********************************************************************/
50
51if (!empty($_POST)) {
52    // Validate submitted data.
53    validateInput();
54    if ($fv->anyErrors()) {
55        // Re-populate form values with the user data so they can make corrections.
56        $frm = resetForm(getFormData());
57    } else {
58        // Setup email object.
59        $email = new Email(array(
60            'to' => $email_to,
61            'from' => sprintf('%s <%s>', getFormData('name'), getFormData('email')),
62            'subject' => 'Contact form email',
63        ));
64        $email->setTemplate('contact.eml');
65        $email->replace(array(
66            'name' => getFormData('name'),
67            'email' => getFormData('email'),
68            'message' => getFormData('message'),
69        ));
70        // Send email!
71        $email->send();
72
73        // Reset form values to display a blank form.
74        $frm = resetForm();
75        $app->raiseMsg(_("<strong>Thank You!</strong> Your message has been sent."), MSG_SUCCESS, __FILE__, __LINE__);
76    }
77} else {
78    // Reset form values to display a blank form.
79    $frm = resetForm();
80}
81
82
83/********************************************************************
84* OUTPUT
85********************************************************************/
86
87include 'header.ihtml';
88include 'contact.ihtml';
89include 'footer.ihtml';
90
91
92/********************************************************************
93* FUNCTIONS
94********************************************************************/
95
96function resetForm($new_values = array())
97{
98    $default = array(
99        'name' => '',
100        'email' => '',
101        'message' => '',
102    );
103    return array_merge($default, $new_values);
104}
105
106function validateInput()
107{
108    global $fv;
109
110    $fv->notEmpty('name', sprintf(_("%s cannot be blank."), _("Name")));
111    $fv->stringLength('name', 0, 255, sprintf(_("%s must be %d-to-%d characters in length."), _("Name"), 0, 255));
112   
113    $fv->notEmpty('email', sprintf(_("%s cannot be blank."), _("Email")));
114    $fv->stringLength('email', 0, 255, sprintf(_("%s must be %d-to-%d characters in length."), _("Email"), 0, 255));
115    $fv->validateEmail('email');
116   
117    $fv->notEmpty('message', sprintf(_("%s cannot be blank."), _("Message")));
118    $fv->stringLength('message', 0, 32000, sprintf(_("%s must be %d-to-%d characters in length."), _("Message"), 0, 32000));
119}
120
121?>
Note: See TracBrowser for help on using the repository browser.