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