10d532d3311ea3cfcce986863f71a3eadc59fe35
[dylansserver.git] / includes / ajax.js
1 $(document).ready(function() {
2 $('#recaptcha_widget').show();
3 $('.submit').click(function() {
4 if ($('#comment_text').val() != '') {
5 var challenge = Recaptcha.get_challenge();
6 var response = Recaptcha.get_response();
7 var captcha_data = { "challenge" : challenge,
8 "response" : response};
9 $.ajax({
10 type: "GET",
11 url: "/captcha",
12 data: captcha_data,
13 success: function(data) {
14 if (data.split('\n')[0] == 'true') {
15 var name = $("#comment_name").val();
16 var email = $("#comment_email").val();
17 var text = $("#comment_text").val();
18 var comment_data = { "captcha" : "passed",
19 "name" : name,
20 "email" : email,
21 "text" : text};
22 $.ajax({
23 type: "POST",
24 // the url may need to be adjusted for
25 // trailing slashes
26 url: "verify",
27 data: comment_data,
28 success: function() {
29 var new_post = "<h3><a href='mailto:" + email
30 + "'>" + name + "</a></h3>"
31 + text + "<br><br>";
32 $('#comments').prepend(new_post);
33 $('#comment').hide();
34 console.log('posted new comment');
35 }
36 });
37 } else {
38 var error = "<span style='font-weight:bold;font-family:sans-serif;color:red;margin-top:15px;'>reCAPTCHA said you're not human</span>";
39 $('#comment').append(error);
40 }
41 },
42 error: function() {
43 console.log('error');
44 },
45 complete: function() {
46 Recaptcha.destroy();
47 }
48 });
49 } else {
50 var error = "<span style='font-weight:bold;font-family:sans-serif;color:red;margin-top:15px;'>but you didn't write anything!<br></span>";
51 $('#submit').before(error);
52 }
53 return false;
54 });
55 });