comment.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. addCommentFormSubmitAction();
  60. },
  61. });
  62. }
  63. function commentsImageReplace(image) {
  64. $('#comment-id').val(image.id);
  65. }
  66. function addCommentFormSubmitAction() {
  67. $('#comment-form').on('submit', commentFormSubmit);
  68. }
  69. $(document).ready(function() {
  70. addCommentFormSubmitAction();
  71. });