|
| ||||||||||
| |||||||||||
| © 2021 DigMusic Onlinefunction submitFormAjax(formId, callback) { const formElement = document.getElementById(formId); if (!formElement) return; formElement.addEventListener('submit', function (event) { event.preventDefault(); event.stopPropagation(); const formData = new FormData(formElement); // Use the form's own action attribute as the endpoint, // falling back to a default if empty const actionUrl = formElement.getAttribute('action') || './ajax_handler.php'; fetch(actionUrl, { method: 'POST', body: formData }) .then(response => response.text()) .then(result => { // Pass the result to a custom callback function for that specific form if (typeof callback === 'function') { callback(result, formElement); } }) .catch(error => { console.error("Fetch error:", error); }); return false; }); } | |||||||||||