ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
example_form.ajax.php
Go to the documentation of this file.
1<?php
2session_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 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 </head>
30<body>
31
32<fieldset>
33<legend>Example Form</legend>
34
35<p class="note">
36 This is an example PHP form that processes user information, checks for errors, and validates the captcha code.<br />
37 This example form also demonstrates how to submit a form to itself to display error messages.
38</p>
39
40<div id="success_message" style="display: none">Your message has been sent!<br />We will contact you as soon as possible.</div>
41
42<form method="post" action="" id="contact_form" onsubmit="return processForm()">
43 <input type="hidden" name="do" value="contact" />
44
45 <p>
46 <strong>Name*:</strong><br />
47 <input type="text" name="ct_name" size="35" value="" />
48 </p>
49
50 <p>
51 <strong>Email*:</strong><br />
52 <input type="text" name="ct_email" size="35" value="" />
53 </p>
54
55 <p>
56 <strong>URL:</strong><br />
57 <input type="text" name="ct_URL" size="35" value="" />
58 </p>
59
60 <p>
61 <strong>Message*:</strong><br />
62 <textarea name="ct_message" rows="12" cols="60"></textarea>
63 </p>
64
65 <p>
66 <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" />
67 <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">
68 <param name="movie" value="./securimage_play.swf?bgcol=#ffffff&amp;icon_file=./images/audio_icon.png&amp;audio_file=./securimage_play.php" />
69 </object>
70 &nbsp;
71 <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 />
72 <strong>Enter Code*:</strong><br />
73 <input type="text" name="ct_captcha" size="12" maxlength="8" />
74 </p>
75
76 <p>
77 <br />
78 <input type="submit" value="Submit Message" />
79 </p>
80
81</form>
82</fieldset>
83
84<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
85<script type="text/javascript">
86 $.noConflict();
87
88 function reloadCaptcha()
89 {
90 jQuery('#siimage').src = './securimage_show.php?sid=' + Math.random();
91 }
92
93 function processForm()
94 {
95 jQuery.ajax({
96 url: '<?php echo $_SERVER['PHP_SELF'] ?>',
97 type: 'POST',
98 data: jQuery('#contact_form').serialize(),
99 dataType: 'json',
100 }).done(function(data) {
101 if (data.error === 0) {
102 jQuery('#success_message').show();
103 jQuery('#contact_form')[0].reset();
105 setTimeout("jQuery('#success_message').fadeOut()", 30000);
106 } else {
107 alert("There was an error with your submission.\n\n" + data.message);
108 }
109 });
110
111 return false;
112 }
113</script>
114
115</body>
116</html>
117
118<?php
119
120// The form processor PHP code
122{
123 if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['do'] == 'contact') {
124 // if the form has been submitted
125
126 foreach($_POST as $key => $value) {
127 if (!is_array($key)) {
128 // sanitize the input data
129 if ($key != 'ct_message') $value = strip_tags($value);
130 $_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
131 }
132 }
133
134 $name = @$_POST['ct_name']; // name from the form
135 $email = @$_POST['ct_email']; // email from the form
136 $URL = @$_POST['ct_URL']; // url from the form
137 $message = @$_POST['ct_message']; // the message from the form
138 $captcha = @$_POST['ct_captcha']; // the user's entry for the captcha code
139 $name = substr($name, 0, 64); // limit name to 64 characters
140
141 $errors = array(); // initialize empty error array
142
143 if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
144 // only check for errors if the form is not in debug mode
145
146 if (strlen($name) < 3) {
147 // name too short, add error
148 $errors['name_error'] = 'Your name is required';
149 }
150
151 if (strlen($email) == 0) {
152 // no email address given
153 $errors['email_error'] = 'Email address is required';
154 } else if ( !preg_match('/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $email)) {
155 // invalid email format
156 $errors['email_error'] = 'Email address entered is invalid';
157 }
158
159 if (strlen($message) < 20) {
160 // message length too short
161 $errors['message_error'] = 'Please enter a message';
162 }
163 }
164
165 // Only try to validate the captcha if the form has no errors
166 // This is especially important for ajax calls
167 if (sizeof($errors) == 0) {
168 require_once dirname(__FILE__) . '/securimage.php';
169 $securimage = new Securimage();
170
171 if ($securimage->check($captcha) == false) {
172 $errors['captcha_error'] = 'Incorrect security code entered';
173 }
174 }
175
176 if (sizeof($errors) == 0) {
177 // no errors, send the form
178 $time = date('r');
179 $message = "A message was submitted from the contact form. The following information was provided.<br /><br />"
180 . "Name: $name<br />"
181 . "Email: $email<br />"
182 . "URL: $URL<br />"
183 . "Message:<br />"
184 . "<pre>$message</pre>"
185 . "<br /><br />IP Address: {$_SERVER['REMOTE_ADDR']}<br />"
186 . "Time: $time<br />"
187 . "Browser: {$_SERVER['HTTP_USER_AGENT']}<br />";
188
189 if (isset($GLOBALS['DEBUG_MODE']) && $GLOBALS['DEBUG_MODE'] == false) {
190 // send the message with mail()
191 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");
192 }
193
194 $return = array('error' => 0, 'message' => 'OK');
195 die(json_encode($return));
196 } else {
197 $errmsg = '';
198 foreach($errors as $key => $error) {
199 // set up error messages to display with each field
200 $errmsg .= " - {$error}\n";
201 }
202
203 $return = array('error' => 1, 'message' => $errmsg);
204 die(json_encode($return));
205 }
206 } // POST
207} // function process_si_contact_form()
SMTP MX.
error($a_errmsg)
set error message @access public
$_POST['username']
Definition: cron.php:12
$GLOBALS['ct_recipient']
processForm()
< 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/>< strong > Enter Code * reloadCaptcha()
process_si_contact_form()
Project: Securimage: A PHP class for creating and managing form CAPTCHA images File: securimage....
$errors