example_form.ajax.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. session_start(); // this MUST be called prior to any output including whitespaces and line breaks!
  3. $GLOBALS['ct_recipient'] = 'YOU@EXAMPLE.COM'; // Change to your email address!
  4. $GLOBALS['ct_msg_subject'] = 'Securimage Test Contact Form';
  5. $GLOBALS['DEBUG_MODE'] = 1;
  6. // CHANGE TO 0 TO TURN OFF DEBUG MODE
  7. // IN DEBUG MODE, ONLY THE CAPTCHA CODE IS VALIDATED, AND NO EMAIL IS SENT
  8. // Process the form, if it was submitted
  9. process_si_contact_form();
  10. ?>
  11. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  12. <html xmlns="http://www.w3.org/1999/xhtml">
  13. <head>
  14. <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
  15. <title>Securimage Example Form</title>
  16. <style type="text/css">
  17. <!--
  18. #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; }
  19. fieldset { width: 90%; }
  20. legend { font-size: 24px; }
  21. .note { font-size: 18px; }
  22. -->
  23. </style>
  24. </head>
  25. <body>
  26. <fieldset>
  27. <legend>Example Form</legend>
  28. <p class="note">
  29. This is an example PHP form that processes user information, checks for errors, and validates the captcha code.<br />
  30. This example form also demonstrates how to submit a form to itself to display error messages.
  31. </p>
  32. <div id="success_message" style="display: none">Your message has been sent!<br />We will contact you as soon as possible.</div>
  33. <form method="post" action="" id="contact_form" onsubmit="return processForm()">
  34. <input type="hidden" name="do" value="contact" />
  35. <p>
  36. <strong>Name*:</strong><br />
  37. <input type="text" name="ct_name" size="35" value="" />
  38. </p>
  39. <p>
  40. <strong>Email*:</strong><br />
  41. <input type="text" name="ct_email" size="35" value="" />
  42. </p>
  43. <p>
  44. <strong>URL:</strong><br />
  45. <input type="text" name="ct_URL" size="35" value="" />
  46. </p>
  47. <p>
  48. <strong>Message*:</strong><br />
  49. <textarea name="ct_message" rows="12" cols="60"></textarea>
  50. </p>
  51. <p>
  52. <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" />
  53. <object type="application/x-shockwave-flash" data="./securimage_play.swf?bgcol=#ffffff&amp;icon_file=./images/audio_icon.png&amp;audio_file=./securimage_play.php" height="32" width="32">
  54. <param name="movie" value="./securimage_play.swf?bgcol=#ffffff&amp;icon_file=./images/audio_icon.png&amp;audio_file=./securimage_play.php" />
  55. </object>
  56. &nbsp;
  57. <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 />
  58. <strong>Enter Code*:</strong><br />
  59. <input type="text" name="ct_captcha" size="12" maxlength="8" />
  60. </p>
  61. <p>
  62. <br />
  63. <input type="submit" value="Submit Message" />
  64. </p>
  65. </form>
  66. </fieldset>
  67. <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
  68. <script type="text/javascript">
  69. $.noConflict();
  70. function reloadCaptcha()
  71. {
  72. jQuery('#siimage').prop('src', './securimage_show.php?sid=' + Math.random());
  73. }
  74. function processForm()
  75. {
  76. jQuery.ajax({
  77. url: '<?php echo $_SERVER['PHP_SELF'] ?>',
  78. type: 'POST',
  79. data: jQuery('#contact_form').serialize(),
  80. dataType: 'json',
  81. }).done(function(data) {
  82. if (data.error === 0) {
  83. jQuery('#success_message').show();
  84. jQuery('#contact_form')[0].reset();
  85. reloadCaptcha();
  86. setTimeout("jQuery('#success_message').fadeOut()", 12000);
  87. } else {
  88. alert("There was an error with your submission.\n\n" + data.message);
  89. }
  90. });
  91. return false;
  92. }
  93. </script>
  94. </body>
  95. </html>
  96. <?php
  97. // The form processor PHP code
  98. function process_si_contact_form()
  99. {
  100. if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['do'] == 'contact') {
  101. // if the form has been submitted
  102. foreach($_POST as $key => $value) {
  103. if (!is_array($key)) {
  104. // sanitize the input data
  105. if ($key != 'ct_message') $value = strip_tags($value);
  106. $_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
  107. }
  108. }
  109. $name = @$_POST['ct_name']; // name from the form
  110. $email = @$_POST['ct_email']; // email from the form
  111. $URL = @$_POST['ct_URL']; // url from the form
  112. $message = @$_POST['ct_message']; // the message from the form
  113. $captcha = @$_POST['ct_captcha']; // the user's entry for the captcha code
  114. $name = substr($name, 0, 64); // limit name to 64 characters
  115. $errors = array(); // initialize empty error array
  116. if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
  117. // only check for errors if the form is not in debug mode
  118. if (strlen($name) < 3) {
  119. // name too short, add error
  120. $errors['name_error'] = 'Your name is required';
  121. }
  122. if (strlen($email) == 0) {
  123. // no email address given
  124. $errors['email_error'] = 'Email address is required';
  125. } else if ( !preg_match('/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $email)) {
  126. // invalid email format
  127. $errors['email_error'] = 'Email address entered is invalid';
  128. }
  129. if (strlen($message) < 20) {
  130. // message length too short
  131. $errors['message_error'] = 'Please enter a message';
  132. }
  133. }
  134. // Only try to validate the captcha if the form has no errors
  135. // This is especially important for ajax calls
  136. if (sizeof($errors) == 0) {
  137. require_once dirname(__FILE__) . '/securimage.php';
  138. $securimage = new Securimage();
  139. if ($securimage->check($captcha) == false) {
  140. $errors['captcha_error'] = 'Incorrect security code entered';
  141. }
  142. }
  143. if (sizeof($errors) == 0) {
  144. // no errors, send the form
  145. $time = date('r');
  146. $message = "A message was submitted from the contact form. The following information was provided.<br /><br />"
  147. . "Name: $name<br />"
  148. . "Email: $email<br />"
  149. . "URL: $URL<br />"
  150. . "Message:<br />"
  151. . "<pre>$message</pre>"
  152. . "<br /><br />IP Address: {$_SERVER['REMOTE_ADDR']}<br />"
  153. . "Time: $time<br />"
  154. . "Browser: {$_SERVER['HTTP_USER_AGENT']}<br />";
  155. if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
  156. // send the message with mail()
  157. 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");
  158. }
  159. $return = array('error' => 0, 'message' => 'OK');
  160. die(json_encode($return));
  161. } else {
  162. $errmsg = '';
  163. foreach($errors as $key => $error) {
  164. // set up error messages to display with each field
  165. $errmsg .= " - {$error}\n";
  166. }
  167. $return = array('error' => 1, 'message' => $errmsg);
  168. die(json_encode($return));
  169. }
  170. } // POST
  171. } // function process_si_contact_form()