Nav apraksta

handleSubmit.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. function handleSubmit(event, formName) {
  2. // Prevent naive submit
  3. event.preventDefault();
  4. // Fetch error description element
  5. var errorDescription = document.querySelector('#error-'+formName+' .error-description');
  6. var submitButton = document.querySelector('#'+formName+' button[type="submit"]');
  7. // Disable button and start "Loading" state
  8. submitButton.disabled = true;
  9. submitButton.classList.remove('btn-danger');
  10. submitButton.classList.add('btn-primary');
  11. submitButton.innerHTML = '<div class="loader"></div>Loading...';
  12. // Fetch all input from form
  13. var fields = {};
  14. // if(formName !== "importQuestionnaire") {
  15. $("form#" + formName).find("input, textarea, select, button[type='submit']").each(function() {
  16. var input = $(this);
  17. fields[input.attr('name')] = input.val();
  18. });
  19. // } else {
  20. // fields = new FormData($('#' + formName));
  21. // https://stackoverflow.com/questions/27936645/send-input-files-data-with-ajax
  22. // }
  23. // Construct request URL (make sure to use http or https accordingly)
  24. var baseURI = document.location.protocol + "//tania.uprrp.edu/admin_nuevo/processes/";
  25. var fullURI = baseURI;
  26. switch(formName) {
  27. // home.php
  28. case "newExperience":
  29. fullURI += "insertExperience.php";
  30. break;
  31. // viewExperience.php
  32. case "editForm":
  33. fullURI += "updateExperience.php";
  34. break;
  35. case "newQuestionnaire":
  36. fullURI += "insertQuestionnaire.php";
  37. break;
  38. // case "importQuestionnaire":
  39. // fullURI += "importQuestionnaire.php";
  40. // break;
  41. case "duplicateQuestionnaire":
  42. fullURI += "duplicateQuestionnaire.php";
  43. break;
  44. case "newMoment":
  45. fullURI += "insertMoment.php";
  46. break;
  47. case "newMilestone":
  48. fullURI += "insertMilestone.php";
  49. break;
  50. // viewQuestionnaire.php
  51. case "updateQuestionnaire":
  52. fullURI += "updateQuestionnaire.php";
  53. break;
  54. case "deleteQuestionnaire":
  55. fullURI += "deleteQuestionnaire.php";
  56. break;
  57. case "newQuestion":
  58. fullURI += "insertQuestionToQuestionnaire.php";
  59. break;
  60. case "deleteQuestion":
  61. fullURI += "deleteQuestion.php";
  62. break;
  63. // viewMoment.php
  64. case "updateMoment":
  65. fullURI += "updateMoment.php";
  66. break;
  67. case "deleteMoment":
  68. fullURI += "deleteMoment.php";
  69. break;
  70. case "removeQuestionFromMoment":
  71. fullURI += "removeQuestionFromMoment.php";
  72. break;
  73. case "addExistingQuestionToMoment":
  74. fullURI += "addExistingQuestionToMoment.php";
  75. break;
  76. // Both viewMoment.php and viewQuestionnaire.php
  77. case "updateQuestion":
  78. fullURI += "updateQuestion.php";
  79. break;
  80. // users.php
  81. case "addUser":
  82. fullURI += "addUser.php";
  83. break
  84. default:
  85. fullURI = document.location.protocol + '//tania.uprrp.edu/admin_nuevo/test.php';
  86. }
  87. // !!!!!! FOR DEBUGGING PURPOSES !!!!!!
  88. console.log(JSON.stringify(fields) + "\n" + fullURI);
  89. // Use fullURI to make POST request w/ jQuery,
  90. // Wait for response (start "Loading" state),
  91. // Act according to error or success
  92. // !!!!!! SHOULD ADD TIMEOUT TO REQUESTS TO !!!!!!!
  93. $.post(fullURI, fields)
  94. .done(function(data, text) {
  95. // !!!!!! FOR DEBUGGING PURPOSES !!!!!!
  96. console.log(data + "and" + text);
  97. // Fetch needed elements
  98. let alert = document.querySelector('#error-'+formName+' .alert');
  99. let errorLead = document.querySelector('#error-'+formName+' .error-lead');
  100. // Clear errors and change message to "Success"
  101. errorDescription.remove();
  102. errorLead.innerText = "Success!";
  103. errorLead.style.marginBottom = "0";
  104. // Change alert style to "Success"
  105. alert.classList.remove('alert-danger');
  106. alert.classList.add('alert-success');
  107. // Change button style to "Success"
  108. submitButton.classList.remove('btn-primary');
  109. submitButton.classList.add('btn-success');
  110. submitButton.innerHTML = '<span class="glyphicon glyphicon-ok"></span> Success!';// could use &#10004;
  111. // Reload page
  112. setTimeout(function() {
  113. let deleteForms = ["deleteQuestionnaire", "deleteMoment", "deleteExperience"];
  114. if(deleteForms.includes(formName)) {
  115. history.back(); // go back to viewExperience page (remember we don't know the ID of the experience it came from)
  116. } else {
  117. location.reload();
  118. }
  119. }, 250);
  120. })
  121. .fail(function(request, status, error) {
  122. // !!!!!! FOR DEBUGGING PURPOSES !!!!!!
  123. console.log(request.responseText);
  124. console.log(status);
  125. console.log(error);
  126. // Fetch needed elements
  127. let alertContainer = document.getElementById('error-'+formName);
  128. // If response is not a JSON, or if JSON doesn't have "error" field, say so
  129. // else, dynamically update error message according to server
  130. if(!isJSON(request.responseText) || JSON.parse(request.responseText).error === undefined) {
  131. errorDescription.innerText = "Something went wrong on our side! Please try again later.";
  132. } else {
  133. errorDescription.innerText = JSON.parse(request.responseText).error;
  134. }
  135. // Change button style to "Retry"
  136. submitButton.innerHTML = "Retry";
  137. submitButton.disabled = false;
  138. // Display alert
  139. alertContainer.style.display = "block";
  140. });
  141. // .always(function(data, textStatus, errorThrown) {
  142. // console.log(data);
  143. // console.log(textStatus);
  144. // console.log(errorThrown);
  145. // });
  146. }