comment.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. function commentFormSubmit(e) {
  2. e.preventDefault();
  3. var nick = $('#comment-nick').val();
  4. var email = $('#comment-email').val();
  5. var content = $('#comment-content').val();
  6. var commentType = $('#comment-type').val();
  7. var commentId = $('#comment-id').val();
  8. if (!nick) {
  9. alert('Nie podano autora komentarza!');
  10. return;
  11. }
  12. if (!content) {
  13. alert('Nie można wysłać pustego komentarza!');
  14. return;
  15. }
  16. var formData = {
  17. 'comment-nick': nick,
  18. 'comment-email': email,
  19. 'comment-content': content,
  20. 'comment-type': commentType,
  21. 'comment-id': commentId,
  22. 'mode': 'json'
  23. };
  24. $.ajax({
  25. dataType: 'json',
  26. type: 'POST',
  27. data: formData,
  28. url: scriptUrlPrefix+'sendcomment.php',
  29. success: commentSubmitDone,
  30. error: commentSubmitFailed,
  31. });
  32. }
  33. function commentSubmitFailed(data) {
  34. alert('Nie udało się przesłać komentarza do serwera.');
  35. }
  36. function commentSubmitDone(data) {
  37. if (data.result == 'OK') {
  38. alert('Dodano komentarz');
  39. refreshComments();
  40. } else {
  41. alert('Nie udało się dodać komentarza! Komunikat od serwera: ' + data.error);
  42. }
  43. }
  44. function refreshComments() {
  45. var commentType = $('#comment-type').val();
  46. var commentId = $('#comment-id').val();
  47. $('#comments').text('Wczytywanie komentarzy...');
  48. var formData = {
  49. 'type': commentType,
  50. 'id': commentId,
  51. };
  52. $.ajax({
  53. dataType: 'html',
  54. type: 'GET',
  55. data: formData,
  56. url: scriptUrlPrefix+'getcomments.php',
  57. success: function(data) {
  58. $('#comments').html(data);
  59. },
  60. });
  61. }
  62. function commentsImageReplace(image) {
  63. $('#comment-id').val(image.id);
  64. }
  65. $(document).ready(function() {
  66. $('#comment-form').on('submit', commentFormSubmit);
  67. });