More cleanup & formatting
[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 if (name == '') { name = "anon" }
19 var comment_data = { "captcha" : "passed",
20 "name" : name,
21 "email" : email,
22 "text" : text};
23 $.ajax({
24 type: "POST",
25 // the url may need to be adjusted for
26 // trailing slashes
27 url: "verify",
28 data: comment_data,
29 success: function() {
30 var new_post = "<h3><a href='mailto:" + email
31 + "'>" + name + "</a></h3>"
32 + text + "<br><br>";
33 $('#comments').prepend(new_post);
34 $('#comment').hide();
35 console.log('posted new comment');
36 }
37 });
38 } else {
39 var error = "<span style='font-weight:bold;font-family:sans-serif;color:red;margin-top:15px;'>reCAPTCHA said you're not human</span>";
40 $('#comment').append(error);
41 }
42 },
43 error: function() {
44 console.log('error');
45 },
46 complete: function() {
47 Recaptcha.destroy();
48 }
49 });
50 } else {
51 var error = "<span style='font-weight:bold;font-family:sans-serif;color:red;margin-top:15px;'>but you didn't write anything!<br></span>";
52 $('#submit').before(error);
53 }
54 return false;
55 });
56 });