4 $GLOBALS[
'ct_recipient'] =
'YOU@EXAMPLE.COM';
5 $GLOBALS[
'ct_msg_subject'] =
'Securimage Test Contact Form';
16 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
20 <style type=
"text/css">
22 #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; }
23 fieldset { width: 90%; }
24 legend { font-size: 24px; }
25 .note { font-size: 18px; }
29 <script src=
"https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
31 <script type=
"text/javascript">
32 function reloadCaptcha()
34 document.getElementById(
'siimage').src =
'./securimage_show.php?sid=' + Math.random();
37 function processForm()
39 new Ajax.Request(
'<?php echo $_SERVER['PHP_SELF
'] ?>', {
41 parameters: $(
'contact_form').serialize(),
42 onSuccess:
function(transport) {
44 var r = transport.responseText.evalJSON();
47 $(
'success_message').show();
48 $(
'contact_form').reset();
50 setTimeout(
"$('success_message').hide()", 30000);
52 alert(
"There was an error with your submission.\n\n" + r.message);
55 alert(
"There was an error parsing the json");
58 onFailure:
function(err) {
59 alert(
"Ajax request failed");
70 <legend>Example Form</legend>
73 This is an example
PHP form that processes user information, checks
for errors, and validates the captcha code.<br />
74 This example form also demonstrates how to submit a form to itself to display error messages.
77 <div
id=
"success_message" style=
"display: none">Your message has been sent!<br />We will contact you as soon as possible.</div>
79 <form method=
"post" action=
"" id=
"contact_form" onsubmit=
"return processForm()">
80 <input type=
"hidden" name=
"do" value=
"contact" />
83 <strong>Name*:</strong><br />
84 <input type=
"text" name=
"ct_name" size=
"35" value=
"" />
88 <strong>Email*:</strong><br />
89 <input type=
"text" name=
"ct_email" size=
"35" value=
"" />
93 <strong>URL:</strong><br />
94 <input type=
"text" name=
"ct_URL" size=
"35" value=
"" />
98 <strong>Message*:</strong><br />
99 <textarea name=
"ct_message" style=
"width: 450px; height: 200px"></textarea>
103 <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">
104 <
object type=
"application/x-shockwave-flash" data=
"./securimage_play.swf?audio_file=./securimage_play.php&bgColor1=#fff&bgColor2=#fff&iconColor=#777&borderWidth=1&borderColor=#000" height=
"32" width=
"32">
105 <param name=
"movie" value=
"./securimage_play.swf?audio_file=./securimage_play.php&bgColor1=#fff&bgColor2=#fff&iconColor=#777&borderWidth=1&borderColor=#000">
108 <a tabindex=
"-1" style=
"border-style: none;" href=
"#" title=
"Refresh Image" onclick=
"reloadCaptcha(); this.blur(); return false"><img src=
"./images/refresh.png" alt=
"Reload Image" onclick=
"this.blur()" align=
"bottom" border=
"0"></a><br />
109 <strong>Enter Code*:</strong><br />
110 <input type=
"text" name=
"ct_captcha" size=
"12" maxlength=
"8" />
115 <input type=
"submit" value=
"Submit Message">
129 if ($_SERVER[
'REQUEST_METHOD'] ==
'POST' && @
$_POST[
'do'] ==
'contact') {
132 foreach(
$_POST as $key => $value) {
133 if (!is_array($key)) {
135 if ($key !=
'ct_message') $value = strip_tags($value);
136 $_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
140 $name = @
$_POST[
'ct_name'];
141 $email = @
$_POST[
'ct_email'];
143 $message = @
$_POST[
'ct_message'];
144 $captcha = @
$_POST[
'ct_captcha'];
145 $name = substr($name, 0, 64);
152 if (strlen($name) < 3) {
154 $errors[
'name_error'] =
'Your name is required';
157 if (strlen($email) == 0) {
159 $errors[
'email_error'] =
'Email address is required';
160 }
else if ( !preg_match(
'/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $email)) {
162 $errors[
'email_error'] =
'Email address entered is invalid';
165 if (strlen($message) < 20) {
167 $errors[
'message_error'] =
'Please enter a message';
174 require_once dirname(__FILE__) .
'/securimage.php';
177 if ($securimage->check($captcha) ==
false) {
178 $errors[
'captcha_error'] =
'Incorrect security code entered';
185 $message =
"A message was submitted from the contact form. The following information was provided.<br /><br />"
186 .
"Name: $name<br />"
187 .
"Email: $email<br />"
190 .
"<pre>$message</pre>"
191 .
"<br /><br />IP Address: {$_SERVER['REMOTE_ADDR']}<br />"
192 .
"Time: $time<br />"
193 .
"Browser: {$_SERVER['HTTP_USER_AGENT']}<br />";
197 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");
200 $return = array(
'error' => 0,
'message' =>
'OK');
201 die(json_encode($return));
204 foreach(
$errors as $key => $error) {
206 $errmsg .=
" - {$error}\n";
209 $return = array(
'error' => 1,
'message' => $errmsg);
210 die(json_encode($return));