function monitorForms() {
// Get all form elements on the page
const forms = document.querySelectorAll('form');
// Loop through each form
forms.forEach(form => {
// Add an event listener to the form's submit event
form.addEventListener('submit', function(event) {
// Prevent the default form submission behavior (page reload)
event.preventDefault();
// Get the form's data
const formData = new FormData(form);
// Display the form data in the console. Replace with desired action
console.log('Form submitted manually:');
for (const [key, value] of formData) {
console.log(`${key}: ${value}`);
}
// Optionally: Perform other actions with the form data here.
// For example, send it to a server using fetch (but this is not async)
// fetch('/your-endpoint', {
// method: 'POST',
// body: formData
// })
// .then(response => {
// // Handle the response
// console.log(response);
// });
});
});
}
// Call the function to start monitoring forms
monitorForms();
Add your comment