Açıklama Yok

handleSubmit.js 4.7KB

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