process.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. window.addEventListener('click', (e) => {
  3. if (e.target.id === 'find') {
  4. getWords();
  5. e.preventDefault();
  6. }
  7. });
  8. function getWords() {
  9. const xhr = new XMLHttpRequest();
  10. const formData = new FormData(document.forms.process_form);
  11. const resultsContainer = document.getElementById("result");
  12. const resultsVerdict = document.getElementById("result_summary");
  13. const resultsList = document.getElementById("result_list");
  14. xhr.open("POST", "src/get_words_list.php");
  15. xhr.onload = function (e) {
  16. if (xhr.readyState == 4 && xhr.status == 200) {
  17. /* Clean previous results if they were shown */
  18. resultsVerdict.innerHTML = "";
  19. resultsList.innerHTML = "";
  20. /* Get response and decode it */
  21. let response = JSON.parse(xhr.response);
  22. /* Get summary message from response */
  23. resultsVerdict.innerHTML = response.verdict;
  24. /* Fill results list with found words */
  25. if (response.count > 0) {
  26. for (let word of response.words) {
  27. let wordListElement = document.createElement("li");
  28. wordListElement.innerHTML = word;
  29. resultsList.appendChild(wordListElement);
  30. }
  31. }
  32. /* If results container is hidden, show it */
  33. resultsContainer.classList.remove("hidden");
  34. }
  35. };
  36. xhr.send(formData);
  37. }