ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
example_form.ajax.php
Go to the documentation of this file.
1 <?php
2 session_start(); // this MUST be called prior to any output including whitespaces and line breaks!
3 
4 $GLOBALS['ct_recipient'] = 'YOU@EXAMPLE.COM'; // Change to your email address!
5 $GLOBALS['ct_msg_subject'] = 'Securimage Test Contact Form';
6 
7 $GLOBALS['DEBUG_MODE'] = 1;
8 // CHANGE TO 0 TO TURN OFF DEBUG MODE
9 // IN DEBUG MODE, ONLY THE CAPTCHA CODE IS VALIDATED, AND NO EMAIL IS SENT
10 
11 
12 // Process the form, if it was submitted
14 
15 ?>
16 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
17 <html>
18 <head>
19  <title>Securimage Example Form</title>
20  <style type="text/css">
21  <!--
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; }
26  -->
27  </style>
28 
29  <script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
30 
31  <script type="text/javascript">
32  function reloadCaptcha()
33  {
34  document.getElementById('siimage').src = './securimage_show.php?sid=' + Math.random();
35  }
36 
37  function processForm()
38  {
39  new Ajax.Request('<?php echo $_SERVER['PHP_SELF'] ?>', {
40  method: 'post',
41  parameters: $('contact_form').serialize(),
42  onSuccess: function(transport) {
43  try {
44  var r = transport.responseText.evalJSON();
45 
46  if (r.error == 0) {
47  $('success_message').show();
48  $('contact_form').reset();
49  reloadCaptcha();
50  setTimeout("$('success_message').hide()", 30000);
51  } else {
52  alert("There was an error with your submission.\n\n" + r.message);
53  }
54  } catch(ex) {
55  alert("There was an error parsing the json");
56  }
57  },
58  onFailure: function(err) {
59  alert("Ajax request failed");
60  }
61  });
62 
63  return false;
64  }
65  </script>
66 </head>
67 <body>
68 
69 <fieldset>
70 <legend>Example Form</legend>
71 
72 <p class="note">
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.
75 </p>
76 
77 <div id="success_message" style="display: none">Your message has been sent!<br />We will contact you as soon as possible.</div>
78 
79 <form method="post" action="" id="contact_form" onsubmit="return processForm()">
80  <input type="hidden" name="do" value="contact" />
81 
82  <p>
83  <strong>Name*:</strong><br />
84  <input type="text" name="ct_name" size="35" value="" />
85  </p>
86 
87  <p>
88  <strong>Email*:</strong><br />
89  <input type="text" name="ct_email" size="35" value="" />
90  </p>
91 
92  <p>
93  <strong>URL:</strong><br />
94  <input type="text" name="ct_URL" size="35" value="" />
95  </p>
96 
97  <p>
98  <strong>Message*:</strong><br />
99  <textarea name="ct_message" style="width: 450px; height: 200px"></textarea>
100  </p>
101 
102  <p>
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&amp;bgColor1=#fff&amp;bgColor2=#fff&amp;iconColor=#777&amp;borderWidth=1&amp;borderColor=#000" height="32" width="32">
105  <param name="movie" value="./securimage_play.swf?audio_file=./securimage_play.php&amp;bgColor1=#fff&amp;bgColor2=#fff&amp;iconColor=#777&amp;borderWidth=1&amp;borderColor=#000">
106  </object>
107  &nbsp;
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" />
111  </p>
112 
113  <p>
114  <br />
115  <input type="submit" value="Submit Message">
116  </p>
117 
118 </form>
119 </fieldset>
120 
121 </body>
122 </html>
123 
124 <?php
125 
126 // The form processor PHP code
127 function process_si_contact_form()
128 {
129  if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['do'] == 'contact') {
130  // if the form has been submitted
131 
132  foreach($_POST as $key => $value) {
133  if (!is_array($key)) {
134  // sanitize the input data
135  if ($key != 'ct_message') $value = strip_tags($value);
136  $_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
137  }
138  }
139 
140  $name = @$_POST['ct_name']; // name from the form
141  $email = @$_POST['ct_email']; // email from the form
142  $URL = @$_POST['ct_URL']; // url from the form
143  $message = @$_POST['ct_message']; // the message from the form
144  $captcha = @$_POST['ct_captcha']; // the user's entry for the captcha code
145  $name = substr($name, 0, 64); // limit name to 64 characters
146 
147  $errors = array(); // initialize empty error array
148 
149  if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
150  // only check for errors if the form is not in debug mode
151 
152  if (strlen($name) < 3) {
153  // name too short, add error
154  $errors['name_error'] = 'Your name is required';
155  }
156 
157  if (strlen($email) == 0) {
158  // no email address given
159  $errors['email_error'] = 'Email address is required';
160  } else if ( !preg_match('/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $email)) {
161  // invalid email format
162  $errors['email_error'] = 'Email address entered is invalid';
163  }
164 
165  if (strlen($message) < 20) {
166  // message length too short
167  $errors['message_error'] = 'Please enter a message';
168  }
169  }
170 
171  // Only try to validate the captcha if the form has no errors
172  // This is especially important for ajax calls
173  if (sizeof($errors) == 0) {
174  require_once dirname(__FILE__) . '/securimage.php';
175  $securimage = new Securimage();
176 
177  if ($securimage->check($captcha) == false) {
178  $errors['captcha_error'] = 'Incorrect security code entered';
179  }
180  }
181 
182  if (sizeof($errors) == 0) {
183  // no errors, send the form
184  $time = date('r');
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 />"
188  . "URL: $URL<br />"
189  . "Message:<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 />";
194 
195  if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
196  // send the message with mail()
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");
198  }
199 
200  $return = array('error' => 0, 'message' => 'OK');
201  die(json_encode($return));
202  } else {
203  $errmsg = '';
204  foreach($errors as $key => $error) {
205  // set up error messages to display with each field
206  $errmsg .= " - {$error}\n";
207  }
208 
209  $return = array('error' => 1, 'message' => $errmsg);
210  die(json_encode($return));
211  }
212  } // POST
213 } // function process_si_contact_form()