1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php

class ContactPop {
    /****** config *********/

    // the title of the emails that the form sends
    var $emailTitle = 'MYSITE Contact Form';

    var $thankYouMessage = "Thank you for contacting us, we'll get back to you shortly.";

    /******* end config ********/


    var $error = '';

    function getFormHtml($ajax = 0) {

        $postedName = $_POST['name'];
        $postedEmail = $_POST['email'];
        $postedMessage = $_POST['message'];

        $sendto = $_POST['subject'];
        if ($sendto == 'option1')
        { $recipient = '[email protected]'; }
        elseif ($sendto == 'option2')
        { $recipient = '[email protected]'; }
        elseif ($sendto == 'option3')
        { $recipient = '[email protected]'; }
        elseif ($sendto == 'option4')
        { $recipient = '[email protected]'; }
        elseif ($sendto == 'optoin5')
        { $recipient = '[email protected]'; }


        $formHtml = '';

        // send congratulations message
        if ( isset($_POST['httpReferer']) && !$this->error ) {
            $out = '<p id="contact-pop-error" class="formItem">' . $this->thankYouMessage . '</p>';

            if ( $ajax ) $out .= '<a href="#" class="close-overlay">Close</a>';

            return $out;
        }

        if ( $this->error ) $formHtml .= '<p id="contact-pop-error" class="formItem">' . $this->error . '</p>';


        $httprefi = $_SERVER["HTTP_REFERER"];

        $cancelLink = $ajax ? '<a href="#" class="close-overlay">Cancel</a>' : '';

        $formHtml .= <<<EOT
        
        <input type="hidden" name="httpReferer" value="$httprefi" />
        
        <div class="formItem">
            <label>Pick a relevant subject:</label>
            <select class="inputText2" name="subject">
            <option class="dropdown" value="option1">option1</option>
            <option class="dropdown" value="option2">option2</option>
            <option class="dropdown" value="option3">option3</option>
            <option class="dropdown" value="option4">option4</option>
            <option class="dropdown" value="option5">option5</option>
            </select>
        </div>
        
        <div class="formItem">
            <label>Name:</label>
            <input type="text" name="name" class="inputText" value="$postedName" size="35" />
        </div>
        
        <div class="formItem">
            <label>Company:</label>
            <input type="text" name="company" class="inputText" value="$postedCompany" size="35" />
        </div>
        
        <div class="formItem">
            <label>Email:</label>
            <input type="text" name="email" class="inputText" value="$postedEmail" size="35" />
        </div>
        
        <div class="formItem">
            <label>Website:</label>
            <input type="text" name="website" class="inputText" value="$postedWebsite" size="35" />
        </div>
        
        <div class="formItem">
            <label>Message:</label>
            <textarea name="message" class="textarea" rows="7" cols="38">$postedMessage</textarea>
        </div>
        
        <div class="formItem">
            <input type="submit" value="Send Message" class="submit" />
        </div>
        
EOT;

        return $formHtml;
    }

    function checkEmail($emailAddress) {
        if (preg_match('/[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/i', $emailAddress)){
            $emailArray = explode("@",$emailAddress);
            if (checkdnsrr($emailArray[1])){
                return TRUE;
            }
        }
        return false;
    }

    function processForm() {

        // check data
        if ( !$_POST['name'] ) $this->error .= 'Please enter your name<br />';
        if ( !$this->checkEmail( $_POST['email'] ) ) $this->error .= 'Please enter a valid email address<br />'; 
        if ( !$_POST['message'] ) $this->error .= 'Please enter a message<br />';

        if ( !$this->error ) $this->sendFormEmail();
    }

    function sendFormEmail() {
        $message = "Name: " . stripslashes($_POST['name']) . 
            "\nEmail: " . $_POST['email'] . 
            "\n\nMessage: " . stripslashes($_POST['message']);

        if ( $_POST['ajaxForm'] ) $message .= "\n\nFrom a Contact-Pop Form on page: " . $_SERVER["HTTP_REFERER"];
        else $message .= "\n\nReferrer: " . $_POST['httpReferer'];

        $message .= "\n\nBrowser Info: " . $_SERVER["HTTP_USER_AGENT"] .
            "\nIP: " . $_SERVER["REMOTE_ADDR"] .
            "\n\nDate: " . date("Y-m-d h:i:s");

        mail($this->sendto, $this->emailTitle, $message, 'From: ' . $_POST['name'] . ' <' . $_POST['email'] . '>');
    }
}

$contactPop = new ContactPop();

if (isset($_POST['httpReferer'])) $contactPop->processForm();

// echo the ajax version of the form
if ( isset($_REQUEST['ajaxForm']) && $_REQUEST['ajaxForm']) {
    echo $contactPop->getFormHtml(1);
}
// or echo the full page version of the form
else {

?>

    <?=$contactPop->getFormHtml(); ?>

<?php 
}
?>