1. function monitorForms() {
  2. // Get all form elements on the page
  3. const forms = document.querySelectorAll('form');
  4. // Loop through each form
  5. forms.forEach(form => {
  6. // Add an event listener to the form's submit event
  7. form.addEventListener('submit', function(event) {
  8. // Prevent the default form submission behavior (page reload)
  9. event.preventDefault();
  10. // Get the form's data
  11. const formData = new FormData(form);
  12. // Display the form data in the console. Replace with desired action
  13. console.log('Form submitted manually:');
  14. for (const [key, value] of formData) {
  15. console.log(`${key}: ${value}`);
  16. }
  17. // Optionally: Perform other actions with the form data here.
  18. // For example, send it to a server using fetch (but this is not async)
  19. // fetch('/your-endpoint', {
  20. // method: 'POST',
  21. // body: formData
  22. // })
  23. // .then(response => {
  24. // // Handle the response
  25. // console.log(response);
  26. // });
  27. });
  28. });
  29. }
  30. // Call the function to start monitoring forms
  31. monitorForms();

Add your comment