Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
example_form.ajax.php
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
13 process_si_contact_form();
14 
15 ?>
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">
18 <head>
19  <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
20  <title>Securimage Example Form</title>
21  <style type="text/css">
22  <!--
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; }
27  -->
28  </style>
29 
30  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
31 
32  <script type="text/javascript">
33  function reloadCaptcha()
34  {
35  document.getElementById('siimage').src = './securimage_show.php?sid=' + Math.random();
36  }
37 
38  function processForm()
39  {
40  new Ajax.Request('<?php echo $_SERVER['PHP_SELF'] ?>', {
41  method: 'post',
42  parameters: $('contact_form').serialize(),
43  onSuccess: function(transport) {
44  try {
45  var r = transport.responseText.evalJSON();
46 
47  if (r.error == 0) {
48  $('success_message').show();
49  $('contact_form').reset();
50  reloadCaptcha();
51  setTimeout("$('success_message').hide()", 30000);
52  } else {
53  alert("There was an error with your submission.\n\n" + r.message);
54  }
55  } catch(ex) {
56  alert("There was an error parsing the json");
57  }
58  },
59  onFailure: function(err) {
60  alert("Ajax request failed");
61  }
62  });
63 
64  return false;
65  }
66  </script>
67 </head>
68 <body>
69 
70 <fieldset>
71 <legend>Example Form</legend>
72 
73 <p class="note">
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.
76 </p>
77 
78 <div id="success_message" style="display: none">Your message has been sent!<br />We will contact you as soon as possible.</div>
79 
80 <form method="post" action="" id="contact_form" onsubmit="return processForm()">
81  <input type="hidden" name="do" value="contact" />
82 
83  <p>
84  <strong>Name*:</strong><br />
85  <input type="text" name="ct_name" size="35" value="" />
86  </p>
87 
88  <p>
89  <strong>Email*:</strong><br />
90  <input type="text" name="ct_email" size="35" value="" />
91  </p>
92 
93  <p>
94  <strong>URL:</strong><br />
95  <input type="text" name="ct_URL" size="35" value="" />
96  </p>
97 
98  <p>
99  <strong>Message*:</strong><br />
100  <textarea name="ct_message" rows="12" cols="60"></textarea>
101  </p>
102 
103  <p>
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&amp;icon_file=./images/audio_icon.png&amp;audio_file=./securimage_play.php" height="32" width="32">
106  <param name="movie" value="./securimage_play.swf?bgcol=#ffffff&amp;icon_file=./images/audio_icon.png&amp;audio_file=./securimage_play.php" />
107  </object>
108  &nbsp;
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" />
112  </p>
113 
114  <p>
115  <br />
116  <input type="submit" value="Submit Message" />
117  </p>
118 
119 </form>
120 </fieldset>
121 
122 </body>
123 </html>
124 
125 <?php
126 
127 // The form processor PHP code
128 function process_si_contact_form()
129 {
130  if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['do'] == 'contact') {
131  // if the form has been submitted
132 
133  foreach($_POST as $key => $value) {
134  if (!is_array($key)) {
135  // sanitize the input data
136  if ($key != 'ct_message') $value = strip_tags($value);
137  $_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
138  }
139  }
140 
141  $name = @$_POST['ct_name']; // name from the form
142  $email = @$_POST['ct_email']; // email from the form
143  $URL = @$_POST['ct_URL']; // url from the form
144  $message = @$_POST['ct_message']; // the message from the form
145  $captcha = @$_POST['ct_captcha']; // the user's entry for the captcha code
146  $name = substr($name, 0, 64); // limit name to 64 characters
147 
148  $errors = array(); // initialize empty error array
149 
150  if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
151  // only check for errors if the form is not in debug mode
152 
153  if (strlen($name) < 3) {
154  // name too short, add error
155  $errors['name_error'] = 'Your name is required';
156  }
157 
158  if (strlen($email) == 0) {
159  // no email address given
160  $errors['email_error'] = 'Email address is required';
161  } else if ( !preg_match('/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $email)) {
162  // invalid email format
163  $errors['email_error'] = 'Email address entered is invalid';
164  }
165 
166  if (strlen($message) < 20) {
167  // message length too short
168  $errors['message_error'] = 'Please enter a message';
169  }
170  }
171 
172  // Only try to validate the captcha if the form has no errors
173  // This is especially important for ajax calls
174  if (sizeof($errors) == 0) {
175  require_once dirname(__FILE__) . '/securimage.php';
176  $securimage = new Securimage();
177 
178  if ($securimage->check($captcha) == false) {
179  $errors['captcha_error'] = 'Incorrect security code entered';
180  }
181  }
182 
183  if (sizeof($errors) == 0) {
184  // no errors, send the form
185  $time = date('r');
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 />"
189  . "URL: $URL<br />"
190  . "Message:<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 />";
195 
196  if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
197  // send the message with mail()
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");
199  }
200 
201  $return = array('error' => 0, 'message' => 'OK');
202  die(json_encode($return));
203  } else {
204  $errmsg = '';
205  foreach($errors as $key => $error) {
206  // set up error messages to display with each field
207  $errmsg .= " - {$error}\n";
208  }
209 
210  $return = array('error' => 1, 'message' => $errmsg);
211  die(json_encode($return));
212  }
213  } // POST
214 } // function process_si_contact_form()