4 $GLOBALS[
'ct_recipient'] =
'YOU@EXAMPLE.COM';
5 $GLOBALS[
'ct_msg_subject'] =
'Securimage Test Contact Form';
7 $GLOBALS[
'DEBUG_MODE'] = 1;
13 process_si_contact_form();
16 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
17 <html xmlns=
"http://www.w3.org/1999/xhtml">
19 <meta http-equiv=
"Content-type" content=
"text/html;charset=UTF-8" />
21 <style type=
"text/css">
23 #success_message { border: 1px solid #000; width: 550px; text-align: left; padding: 10px 7px; background: #33ff33; color: #000; font-weight; bold; font-size: 1.2em; border-radius: 4px; -moz-border-radius: 4px; -webkit-border-radius: 4px; }
24 fieldset { width: 90%; }
25 legend { font-size: 24px; }
26 .note { font-size: 18px; }
30 <script type=
"text/javascript" src=
"https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
32 <script type=
"text/javascript">
33 function reloadCaptcha()
35 document.getElementById(
'siimage').src =
'./securimage_show.php?sid=' + Math.random();
38 function processForm()
40 new Ajax.Request(
'<?php echo $_SERVER['PHP_SELF
'] ?>', {
42 parameters: $(
'contact_form').serialize(),
43 onSuccess:
function(transport) {
45 var r = transport.responseText.evalJSON();
48 $(
'success_message').show();
49 $(
'contact_form').reset();
51 setTimeout(
"$('success_message').hide()", 30000);
53 alert(
"There was an error with your submission.\n\n" + r.message);
56 alert(
"There was an error parsing the json");
59 onFailure:
function(err) {
60 alert(
"Ajax request failed");
71 <legend>Example Form</legend>
74 This is an example PHP form that processes user information, checks
for errors, and validates the captcha code.<br />
75 This example form also demonstrates how to submit a form to itself to display error messages.
78 <div
id=
"success_message" style=
"display: none">Your message has been sent!<br />We will contact you as soon as possible.</div>
80 <form method=
"post" action=
"" id=
"contact_form" onsubmit=
"return processForm()">
81 <input type=
"hidden" name=
"do" value=
"contact" />
84 <strong>
Name*:</strong><br />
85 <input type=
"text" name=
"ct_name" size=
"35" value=
"" />
89 <strong>Email*:</strong><br />
90 <input type=
"text" name=
"ct_email" size=
"35" value=
"" />
94 <strong>URL:</strong><br />
95 <input type=
"text" name=
"ct_URL" size=
"35" value=
"" />
99 <strong>Message*:</strong><br />
100 <textarea name=
"ct_message" rows=
"12" cols=
"60"></textarea>
104 <img
id=
"siimage" style=
"border: 1px solid #000; margin-right: 15px" src=
"./securimage_show.php?sid=<?php echo md5(uniqid()) ?>" alt=
"CAPTCHA Image" align=
"left" />
105 <
object type=
"application/x-shockwave-flash" data=
"./securimage_play.swf?bgcol=#ffffff&icon_file=./images/audio_icon.png&audio_file=./securimage_play.php" height=
"32" width=
"32">
106 <param name=
"movie" value=
"./securimage_play.swf?bgcol=#ffffff&icon_file=./images/audio_icon.png&audio_file=./securimage_play.php" />
109 <a tabindex=
"-1" style=
"border-style: none;" href=
"#" title=
"Refresh Image" onclick=
"document.getElementById('siimage').src = './securimage_show.php?sid=' + Math.random(); this.blur(); return false"><img src=
"./images/refresh.png" alt=
"Reload Image" height=
"32" width=
"32" onclick=
"this.blur()" align=
"bottom" border=
"0" /></a><br />
110 <strong>Enter Code*:</strong><br />
111 <input type=
"text" name=
"ct_captcha" size=
"12" maxlength=
"8" />
116 <input type=
"submit" value=
"Submit Message" />
128 function process_si_contact_form()
130 if ($_SERVER[
'REQUEST_METHOD'] ==
'POST' && @$_POST[
'do'] ==
'contact') {
133 foreach($_POST as $key => $value) {
134 if (!is_array($key)) {
136 if ($key !=
'ct_message') $value = strip_tags($value);
137 $_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
141 $name = @$_POST[
'ct_name'];
142 $email = @$_POST[
'ct_email'];
143 $URL = @$_POST[
'ct_URL'];
144 $message = @$_POST[
'ct_message'];
145 $captcha = @$_POST[
'ct_captcha'];
146 $name = substr($name, 0, 64);
150 if (isset($GLOBALS[
'DEBUG_MODE']) && $GLOBALS[
'DEBUG_MODE'] ==
false) {
153 if (strlen($name) < 3) {
155 $errors[
'name_error'] =
'Your name is required';
158 if (strlen($email) == 0) {
160 $errors[
'email_error'] =
'Email address is required';
161 }
else if ( !preg_match(
'/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $email)) {
163 $errors[
'email_error'] =
'Email address entered is invalid';
166 if (strlen($message) < 20) {
168 $errors[
'message_error'] =
'Please enter a message';
174 if (
sizeof($errors) == 0) {
175 require_once dirname(__FILE__) .
'/securimage.php';
178 if ($securimage->check($captcha) ==
false) {
179 $errors[
'captcha_error'] =
'Incorrect security code entered';
183 if (
sizeof($errors) == 0) {
186 $message =
"A message was submitted from the contact form. The following information was provided.<br /><br />"
187 .
"Name: $name<br />"
188 .
"Email: $email<br />"
191 .
"<pre>$message</pre>"
192 .
"<br /><br />IP Address: {$_SERVER['REMOTE_ADDR']}<br />"
193 .
"Time: $time<br />"
194 .
"Browser: {$_SERVER['HTTP_USER_AGENT']}<br />";
196 if (isset($GLOBALS[
'DEBUG_MODE']) && $GLOBALS[
'DEBUG_MODE'] ==
false) {
198 mail($GLOBALS[
'ct_recipient'], $GLOBALS[
'ct_msg_subject'], $message,
"From: {$GLOBALS['ct_recipient']}\r\nReply-To: {$email}\r\nContent-type: text/html; charset=ISO-8859-1\r\nMIME-Version: 1.0");
201 $return = array(
'error' => 0,
'message' =>
'OK');
202 die(json_encode($return));
205 foreach($errors as $key => $error) {
207 $errmsg .=
" - {$error}\n";
210 $return = array(
'error' => 1,
'message' => $errmsg);
211 die(json_encode($return));